颤振错误:找不到正确的ScopedModel

时间:2018-08-21 04:28:12

标签: dart flutter state flutter-layout scoped-model

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

型号代码为

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

创建作用域模型的配置文件页面如下。在这里,我从该州的Firebase获得了关注者列表,并使用它来创建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。

 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的一部分

    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的小部件位于

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

3 个答案:

答案 0 :(得分:4)

这里的代码将推送一个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的使用将不起作用,因为其上方没有ScopedModelScopedModel仅用ProfileWidget

定义

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

答案 1 :(得分:0)

如果某人有相同的错误,则该错误也可能是上下文。 在某些情况下,它找不到范围模型,因为上下文不在您启动主范围模型的地方

答案 2 :(得分:0)

您需要像这样在 main.dart 中添加 scopedmodel:

@override
  Widget build(BuildContext context){
    return ScopedModel<UserModel>(
        model: UserModel(),
        child: MaterialApp(
            title: "YourApp",
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              body: HomeScreen(),
            )
        )
    );
  }