Dart 颤振错误:找不到正确的ScopedModel
我正试图在我的颤振项目中创建一个scopedmodel,但我似乎不明白为什么会出现错误。此scopedmodel实现中有什么错误? 我有一个带有底部导航器的主页。在profile选项卡中,我在树深处的小部件中获取所需的跟随者列表,因此我尝试使用scopedmodel 模型代码为Dart 颤振错误:找不到正确的ScopedModel,dart,flutter,state,flutter-layout,scoped-model,Dart,Flutter,State,Flutter Layout,Scoped Model,我正试图在我的颤振项目中创建一个scopedmodel,但我似乎不明白为什么会出现错误。此scopedmodel实现中有什么错误? 我有一个带有底部导航器的主页。在profile选项卡中,我在树深处的小部件中获取所需的跟随者列表,因此我尝试使用scopedmodel 模型代码为 class RelationshipsModel extends Model { List<Relationship> relations; RelationshipsMode
class RelationshipsModel extends Model {
List<Relationship> relations;
RelationshipsModel(this.relations);
void add(String name) {
relations.add(new Relationship("", "", name, name));
notifyListeners();
}
}
类关系模型扩展了模型{
列表关系;
关系模型(此为关系);
void add(字符串名称){
添加(新关系(“,”,名称,名称));
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),
],
),
),
])),
)
);
}
}
class ProfileWidget扩展StatefulWidget{
@凌驾
_ProfileWidgetState createState()=>\u ProfileWidgetState();
}
类_ProfileWidgetState扩展状态{
@凌驾
initState(){
super.initState();
getCurrentUserDetails();
}
getCurrentUserDetails()异步{
_name=wait CacheService.getCurrentUser();
var following=wait service.getfollowinlist(_name);
设置状态(){
这个。_following=following;
});
}
@凌驾
小部件构建(构建上下文){
if(this.\u name==null){
退回新货柜(
子项:常量CupertinoActivityIndicator(),
);
}
返回范围模型(
型号:RelationshipsModel(如下所示),
孩子:脚手架(
//更多的东西
背景:新堆栈(子项:[
新的CarouselWidget(),
新对齐(
对齐:分馏Loffset.topCenter,
高度系数:6.0,
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
//在这里,我创建了一个小部件,它显示了追随者和追随者的列表
新FollowerInfoWidget(followers:this.\u followers,
以下内容:本,
],
),
),
])),
)
);
}
}
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),
),
);
}
class\u FollowerState扩展状态{
小部件buildListTile(BuildContext上下文,关系项){
返回新的合并语义(
孩子:新的ListTile(
标题:新文本(项目.followerId),
onTap:()异步{
if(item.followerId==wait CacheService.getCurrentUser()){
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>ProfileWidget()),
);
}
否则{
导航器。推(
上下文
MaterialPage路线(生成器:(上下文)=>
UserWidget(名称:item.followerId)),
);
}
},
尾随:新图标(Icons.info,颜色:Theme.of(context).disabledColor),
),
);
}
followWidget后面的按钮是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 ()
class UserWidget扩展了StatefulWidget{
用户部件(
{Key})
:super(key:key);
@凌驾
_UserWidgetState createState()=>\u UserWidgetState();
}
类_UserWidgetState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:DefaultTabController(
//更多的东西
新FollowerInfoWidget(
追随者:这个,追随者,,
下面:这个,
),
新的FollowUnFollowWidget()
下面是具有ScopedModelSecondant的小部件
class FollowUnFollowWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<RelationshipsModel>(
builder: (context, child, model) =>
FloatingActionButton(
onPressed: () {}
//more stuff
)
);
}
}
类FollowUnFollowWidget扩展了无状态Widget{
@凌驾
小部件构建(构建上下文){
返回范围modeldescendant(
生成器:(上下文、子项、模型)=>
浮动操作按钮(
onPressed:(){}
//更多的东西
)
);
}
}
此处的代码将推送一个视图,该视图是ProfileWidget
或用户Widget
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
定义
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)),
);
}
},
看起来您需要将
ScopedModel
添加到UserWidget
的构建方法顶部,或者作为buildListTile
的一部分。如果有人有相同的错误,错误也可能是上下文。
在某些情况下,它找不到作用域模型,因为上下文不是您启动主作用域模型的位置您需要在main.dart中添加作用域模型,如下所示:
@override
Widget build(BuildContext context){
return ScopedModel<UserModel>(
model: UserModel(),
child: MaterialApp(
title: "YourApp",
debugShowCheckedModeBanner: false,
home: Scaffold(
body: HomeScreen(),
)
)
);
}
@覆盖
小部件构建(构建上下文){
返回范围模型(
模型:UserModel(),
孩子:MaterialApp(
标题:“YourApp”,
debugShowCheckedModeBanner:false,
家:脚手架(
主体:主屏幕(),
)
)
);
}
小部件树中的FollowUnFollowWidget
?它是否在ProfileWidget
的//more-stuff部分?不,它是从另一个小部件调用的,而另一个小部件又是从ProfileWidget调用的。ProfileWidget->UserWidget->FollowUnFollowWidget你能用你的例子来展示你提到的小部件吗ofileWidget表示用户的配置文件,其中显示被跟踪用户的数量和关注者的数量。这两个数字是可单击的文本,显示该列表中的所有用户。从该列表中选择用户时,将调用UserWidget,该小部件还具有跟踪/取消跟踪该用户的按钮。我已编辑