【问题标题】:Error: Could not find the correct ScopedModel错误:找不到正确的 ScopedModel
【发布时间】:2019-03-22 19:19:24
【问题描述】:

我正在开发一个有很多页面的应用程序,为了管理状态我开始使用ScopedModel,这是我的应用程序简单结构:

我的MainModel() 是:

class MainModel extends Model with LoginModel, PostsFeedModel, SocialLoginModel, ProfileModel{}

然后我像这样将它添加到主材质应用程序中:

return ScopedModel<MainModel>(
  model: MainModel(),
  child: new MaterialApp(

我不能简单地在任何子视图上使用ScopedModelDescendant,例如:

child: ScopedModelDescendant&lt;MainModel&gt;( ...我收到这个错误

错误:找不到正确的 ScopedModel

我必须添加ScopedModel&lt;MainModel&gt;(... 才能让它工作!

我播下了这个答案here,但我很确定这是可能的,因为我正在学习一门课程,它的讲师使用非常相似的结构(推动新观点)并且对他有用。

作为另一个尝试使用多个范围模型,我遵循了范围模型所有者的建议 here,如下所示:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:Sheer/util/SheerDialog.dart';
import 'package:scoped_model/scoped_model.dart';
import '../../models/profile-model.dart';

class Profile extends StatefulWidget {
  final uid;

  Profile({@required this.uid});

  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
@override
  Widget build(BuildContext context) {
    final profileModel = ScopedModel.of<ProfileModel>(context);
    return profileModel.user.photoURL != ''
      ? Scaffold( ....

它仍然显示相同的错误,任何帮助将不胜感激。 感谢并原谅我语言不好。

【问题讨论】:

  • 在导入库时,您应该遵循一种模式,使用import 'package: 或仅使用不带包关键字的路径名称

标签: dart flutter


【解决方案1】:

问题在于我导入 main_model.dart 的方式;

在 main.dart 我这样导入它:models/main_model.dart

在我这样导入的内页中:package:MYAPP/models/main_model.dart

当我统一调用文件的方式时,它按预期工作

感谢 Raof Rahich 的评论

【讨论】:

    猜你喜欢
    • 2019-01-27
    • 2019-09-15
    • 2018-05-01
    • 2013-09-23
    • 2014-03-06
    • 2012-09-27
    • 1970-01-01
    • 2023-04-06
    • 2017-06-06
    相关资源
    最近更新 更多