flutter repository provider code example

Example: BlocProvider

import 'package:blocs_without_libraries/app/blocs/edit_todo_bloc.dart';
import 'package:blocs_without_libraries/app/blocs/todos_bloc.dart';
import 'package:flutter/material.dart';

// InheritedWidget objects have the ability to be
// searched for anywhere 'below' them in the widget tree.
class BlocProvider extends InheritedWidget {

  // these blocs are the objects that we want to access throughout the app
  final TodoListBloc todoListBloc;
  final EditTodoBloc editTodoBloc;

  /// Inherited widgets require a child widget
  /// which they implicitly return in the same way
  /// all widgets return other widgets in their 'Widget.build' method.
  const BlocProvider({
    Key key,
    @required Widget child,
    this.todoListBloc,
    this.editTodoBloc,
  })  : assert(child != null),
        super(key: key, child: child);

  /// this method is used to access an instance of
  /// an inherited widget from lower in the tree.
  /// `BuildContext.dependOnInheritedWidgetOfExactType` is a built in
  /// Flutter method that does the hard work of traversing the tree for you
  static BlocProvider of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<BlocProvider>();
  }

  @override
  bool updateShouldNotify(BlocProvider old) {
    return true;
  }
}
----------------------------------------------------------------------------------
void main() {
  var _repository = TodoRepository(WebClient());

  runApp(
    /// Bloc provider is a widget, so it can be used
    /// as the root of your Flutter app!
    BlocProvider(
      todoListBloc: TodoListBloc(_repository),
      editTodoBloc: EditTodoBloc(seed: Task('')),
      child: BlocsWithoutLibrariesApp(),
    ),
  );
}

Tags:

Misc Example