fluttersyntaxblocinherited-widget

Please Explain syntax - flutter bloc provider using inherited widget


I do not understand the constructor part and static function part. super? dependOnInheritedWidgetOfExactType?

import 'comments_bloc.dart';
export 'comments_bloc.dart';

class CommentsProvider extends InheritedWidget {
  final CommentsBloc commentsBloc;
  CommentsProvider({Key key, Widget child})
      : commentsBloc = CommentsBloc(), // what this line is doing.
        super(key: key, child: child);

  bool updateShouldNotify(_) => true;

//below code?
  static CommentsBloc of(BuildContext context) {
    return context
        .dependOnInheritedWidgetOfExactType<CommentsProvider>()
        .commentsBloc;
  }
}

Solution

  • Step 1 : The dependOnInheritedWidgetOfExactType method enables a descendant widget to access the closest ancestor InheritedWidget instance enclosed in its BuildContext, in your code is CommentsProvider

    Step 2 : And .commentsBloc means access this CommentsProvider 's attribute, in your code is final CommentsBloc commentsBloc;