Dart 如何使用提供程序包初始化状态? TL;DR-从使用者获取providerInfo=null( 生成器:(上下文、providerInfo、子级),

Dart 如何使用提供程序包初始化状态? TL;DR-从使用者获取providerInfo=null( 生成器:(上下文、providerInfo、子级),,dart,flutter,state-management,Dart,Flutter,State Management,我有一个Flitter应用程序,它使用的是一个可以正常工作的应用程序,但我想重构它,这样它就可以使用了 具有作用域的\u模型的代码: TL;DR - Getting providerInfo = null from Consumer<ProviderInfo>( builder: (context, providerInfo, child), //导入。。。 void main(){ runApp(MyApp()); } 类MyApp扩展了StatefulWidget{ @

我有一个Flitter应用程序,它使用的是一个可以正常工作的应用程序,但我想重构它,这样它就可以使用了

具有作用域的\u模型的代码:

TL;DR - Getting providerInfo = null from Consumer<ProviderInfo>(
    builder: (context, providerInfo, child),
//导入。。。
void main(){
runApp(MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
状态createState(){
返回_MyAppState();
}
}
类MyAppState扩展了状态{
final MainModel _model=MainModel();//数据类,扩展了作用域为_model.model的类,以及所有其他模型。。。
bool\u isAuthenticated=假;
@凌驾
void initState(){
_model.init();
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回范围模型(
模型:_模型,
孩子:MaterialApp(
标题:“MyApp”,
路线:{
“/”:(BuildContext上下文)=>\u isAuthenticated==false?AuthenticationPage():主页(\u模型),
“/admin”:(构建上下文)=>
_isAuthenticated==false?AuthenticationPage():AdminPage(\u模型),
},
//构建的其余部分。。。
}
以及我试图重构以使用Provider的代码:

//imports...
void main() {
  runApp(MyApp());
}
class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  final MainModel _model = MainModel();// The data class, extends scoped_model.Model class, with all of other models...
  bool _isAuthenticated = false;
  @override
  void initState() {
    _model.init();
    super.initState();
}
@override
  Widget build(BuildContext context) {
    return ScopedModel<MainModel>(
      model: _model,
      child: MaterialApp(
        title: "MyApp",
        routes: {
          '/': (BuildContext context) => _isAuthenticated == false ? AuthenticationPage() : HomePage(_model),
          '/admin': (BuildContext context) =>
              _isAuthenticated == false ? AuthenticationPage() : AdminPage(_model),
        },
// the rest of build...

}
/@lib/main.dart
//进口。。。
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回ChangeNotifierProvider(
生成器:(上下文){
ProviderInfo();//数据模型。
},
儿童:消费者(
生成器:(上下文、providerInfo、子项)=>MaterialApp(
标题:“MyApp”,
路线:{
“/”:(构建上下文){
providerInfo.isAuthenticated==false?AuthenticationPage():主页(providerInfo);
},
“/admin”:(构建上下文){
providerInfo.isAuthenticated==false?AuthenticationPage():AdminPage(\u模型);
},     
//构建的其余部分。。。
},
//@ProviderInfo
类ProviderInfo将CombinedModel与ProductModel、UserModel、UtilityModel一起扩展{
ProviderInfo(){
this.init();
}
}
此代码的问题在于,在
消费者
的builder函数中,
提供者信息
为空(当然,在路由等中也是空的)

我做错了什么?
我如何重构它才能使其正常工作?

您忘记了在提供者的
生成器中返回一些内容

改变

//@lib/main.dart
//imports...
void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ProviderInfo>(
      builder: (context) {
        ProviderInfo(); // the data model. 
      },
      child: Consumer<ProviderInfo>(
        builder: (context, providerInfo, child) => MaterialApp(
              title: "MyApp",
              routes: {
                '/': (BuildContext context) {
                  providerInfo.isAuthenticated == false ? AuthenticationPage() : HomePage(providerInfo);
                },
                '/admin': (BuildContext context) {
                    providerInfo.isAuthenticated == false ? AuthenticationPage() : AdminPage(_model);
                },     
//the rest of build...
              },

//@ProviderInfo
class ProviderInfo extends CombinedModel with ProductModel, UserModel, UtilityModel {

  ProviderInfo() {
    this.init();
  }
}


你的注意力太多了
 ProviderInfo() 
 return ProviderInfo()