Flutter错误:找不到正确的ScopedModel [英] Flutter Error: Could not find the correct ScopedModel

查看:309
本文介绍了Flutter错误:找不到正确的ScopedModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的flutter项目中创建一个scopedmodel,但似乎无法弄清楚为什么会出现此错误.在此scopedmodel实现中出了什么问题? 我有一个带有底部导航器的主页.在配置文件选项卡中,我在树中深处的小部件中获取所需的关注者列表,因此尝试使用scopedmodel.

I am trying to create a scopedmodel in my flutter project and I can't seem to figure out why I get the error. What is wrong in this scopedmodel implementation? I have a home page with bottom navigator. Inside the profile tab, I fetch the list of followers which I need in a widget deep in the tree so I am trying to use scopedmodel.

型号代码为

class RelationshipsModel extends Model {
        List<Relationship> relations;
        RelationshipsModel(this.relations);
        void add(String name) {
            relations.add(new Relationship("", "", name, name));
            notifyListeners();
      }
    }

创建作用域模型的配置文件页面如下.在这里,我从该状态获得了Firebase的关注者列表,并使用它来创建scopedmodel的模型.

Profile page that creates the scopedmodel is below. Here I get the followers list from Firebase in the state and use it to create the model for the scopedmodel.

 class ProfileWidget extends StatefulWidget {
      @override
      _ProfileWidgetState createState() => _ProfileWidgetState();
    }

class _ProfileWidgetState extends State<ProfileWidget> {
@override
  initState() {
    super.initState();
    getCurrentUserDetails();
  }
getCurrentUserDetails() async {
_name = await CacheService.getCurrentUser();
var following = await service.getFollowingList(_name);
setState(() {
      this._following = following;
    });
  }

 @override
  Widget build(BuildContext context) {
    if (this._name == null) {
      return new Container(
        child: const CupertinoActivityIndicator(),
      );
    }

return  ScopedModel<RelationshipsModel>(
        model: RelationshipsModel(this._following),
        child: Scaffold(
       //more stuff

         background: new Stack(children: <Widget>[
                  new CarouselWidget(),
                  new Align(
                    alignment: FractionalOffset.topCenter,
                    heightFactor: 6.0,
                    child: new Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        //Here I am creating the a widget that shows the list of followers and following
                        new FollowerInfoWidget(followers: this._followers,
                            following: this._following),
                      ],
                    ),
                  ),
                ])),


     )
   );
  }
}

FollowerInfoWidget在下面,它根据从列表中单击的用户来调用ProfileWidget或UserWidget.

The FollowerInfoWidget is below which calls the ProfileWidget or UserWidget based on the clicked user from the list

 class _FollowerState extends State<FollowerWidget> {

  Widget buildListTile(BuildContext context, Relationship item) {
    return new MergeSemantics(
      child: new ListTile(

        title: new Text(item.followerId),
        onTap: () async {
          if (item.followerId == await CacheService.getCurrentUser()) {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => ProfileWidget()),
            );
          }
          else {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) =>
                  UserWidget(name: item.followerId)),
            );
          }
        },
        trailing: new Icon(Icons.info, color: Theme.of(context).disabledColor),
      ),
    );
  }

FollowUnFollowWidget按钮是UserWidget的一部分

The button which is FollowUnFollowWidget is part of the UserWidget

    class UserWidget extends StatefulWidget {
  UserWidget(
      {Key key})
      : super(key: key);
  @override
  _UserWidgetState createState() => _UserWidgetState();
}

class _UserWidgetState extends State<UserWidget> {
@override
  Widget build(BuildContext context) {
return Scaffold(
      body: DefaultTabController(
//more stuff
 new FollowerInfoWidget(
                              followers: this._followers,
                              following: this._following,
                            ),
                            new FollowUnFollowWidget ()

具有ScopedModelDescendant的小部件位于

And the widget that has the ScopedModelDescendant is below

class FollowUnFollowWidget extends StatelessWidget {  
  @override
  Widget build(BuildContext context) {
    return ScopedModelDescendant<RelationshipsModel>(
        builder: (context, child, model) =>
            FloatingActionButton(
            onPressed: () {}
           //more stuff
       )
    );
  }
}

推荐答案

此处的代码将推送ProfileWidgetUserWidget

onTap: () async {
      if (item.followerId == await CacheService.getCurrentUser()) {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => ProfileWidget()),
        );
      }
      else {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) =>
              UserWidget(name: item.followerId)),
        );
      }
    },

由于UserWidget具有FollowUnFollowWidget,因此ScopedModelDescendant的使用将不起作用,因为其上方没有ScopedModel.此ScopedModel仅用ProfileWidget

Since UserWidget has the FollowUnFollowWidget the use of ScopedModelDescendant would not work since there is no ScopedModel above it. This ScopedModel is only defined with the ProfileWidget

看起来您需要将ScopedModel添加到UserWidget的构建方法的顶部或作为buildListTile的一部分

Looks like you need to add the ScopedModel to either the top of the build method of UserWidget or as part of buildListTile

这篇关于Flutter错误:找不到正确的ScopedModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆