Flutter Error: Could not find the correct ScopedModel
Flutter Error: Could not find the correct ScopedModel
我正在尝试在我的 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();
}
}
创建 scopedmodel 的配置文件页面如下。在这里,我从该州的 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
)
);
}
}
此处的代码将推送 ProfileWidget
或 UserWidget
的视图
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
定义
看起来您需要将 ScopedModel
添加到 UserWidget
构建方法的顶部或作为 buildListTile
的一部分
如果有人有同样的错误,错误也可能是上下文。
在某些情况下,它找不到 Scoped 模型,因为上下文不是您开始主 ScopedModel
的地方
您需要像这样在 main.dart 中添加作用域模型:
@override
Widget build(BuildContext context){
return ScopedModel<UserModel>(
model: UserModel(),
child: MaterialApp(
title: "YourApp",
debugShowCheckedModeBanner: false,
home: Scaffold(
body: HomeScreen(),
)
)
);
}
我正在尝试在我的 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();
}
}
创建 scopedmodel 的配置文件页面如下。在这里,我从该州的 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
)
);
}
}
此处的代码将推送 ProfileWidget
或 UserWidget
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
看起来您需要将 ScopedModel
添加到 UserWidget
构建方法的顶部或作为 buildListTile
如果有人有同样的错误,错误也可能是上下文。 在某些情况下,它找不到 Scoped 模型,因为上下文不是您开始主 ScopedModel
的地方您需要像这样在 main.dart 中添加作用域模型:
@override
Widget build(BuildContext context){
return ScopedModel<UserModel>(
model: UserModel(),
child: MaterialApp(
title: "YourApp",
debugShowCheckedModeBanner: false,
home: Scaffold(
body: HomeScreen(),
)
)
);
}