【问题标题】:Please Explain syntax - flutter bloc provider using inherited widget请解释语法 - 使用继承小部件的颤振块提供程序
【发布时间】:2020-07-21 18:07:35
【问题描述】:

我不明白构造函数部分和静态函数部分。 极好的? 依赖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;
  }
}

【问题讨论】:

    标签: flutter syntax bloc inherited-widget


    【解决方案1】:

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

    第 2 步:.commentsBloc 表示访问此 CommentsProvider 的属性,在您的代码中为 final CommentsBloc commentsBloc;

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 2021-11-13
      • 2021-11-11
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2019-11-03
      相关资源
      最近更新 更多