0

我不明白构造函数部分和静态函数部分。极好的?依赖InheritedWidgetOfExactType?

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;
  }
}
4

1 回答 1

0

第 1 步:该dependOnInheritedWidgetOfExactType方法使后代小部件能够访问InheritedWidget包含在其中的最近的祖先实例BuildContext,在您的代码中是CommentsProvider

第 2 步:.commentsBloc意味着访问 thisCommentsProvider的属性,在您的代码中是final CommentsBloc commentsBloc;

于 2020-04-09T09:23:26.360 回答