运行应用程序时,HomeNewsPage第一次变成红页, 控制台显示一条错误消息,大约1秒钟,HomeNewsPage恢复正常,我从HomeProvider那里获得了数据。 我一直在寻找为什么找不到原因的原因, 请帮我看看代码,我的英语不是很好,非常感谢,
还有
HomeProvider provider = Provider.of(上下文,听:false );
设置listen = true _HomeNewsPageBuildState# build()方法将执行两次
设置listen = true _HomeNewsPageBuildState# build()方法将执行一次
我不知道为什么
class HomeNewsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
HomeProvider provider = Provider.of<HomeProvider>(context, listen: false);
return _HomeNewsPageBuild(provider);
}
}
class _HomeNewsPageBuild extends StatefulWidget {
final HomeProvider provider;
_HomeNewsPageBuild(this.provider);
@override
_HomeNewsPageBuildState createState() => _HomeNewsPageBuildState();
}
class _HomeNewsPageBuildState extends State<_HomeNewsPageBuild> {
HomeProvider _provide;
@override
void initState() {
print('HomeNewsPage initState');
_provide ??= widget.provider;
getHomeNewsList(_provide);
super.initState();
}
@override
Widget build(BuildContext context) {
print("HomeNewsPage--build");
return Scaffold(
backgroundColor: UniteColors.bg_color,
body: _initView(),
);
}
Consumer<HomeProvider> _initView() {
return Consumer<HomeProvider>(
builder: (_, HomeProvider value, __) {
return _provide.newsList.length > 0 ? _buildListView() : UiHelper.initLoading(false);
},
);
}
Widget _buildListView() {
return ListView(
children: <Widget>[
_newsBigItem(),
Divider(height: 1.0, color: UniteColors.divider_color),
_newsSmallItem(),
],
);
}
}
class HomeProvider extends BaseProvider {
final HomeRepo _homeRepo = HomeRepo();
List<DataList> _newsList;
List<DataList> get newsList => _newsList;
set newsList(List<DataList> value) {
_newsList = value;
notifyListeners();
}
///GetNewsListData
Observable getHomeNewsList() => _homeRepo
.getHomeNewsList()
.doOnData((result) {
var data = json.decode(result.toString());
HomeEntity homeEntity = EntityFactory.generateOBJ(data);
this.newsList = homeEntity.dataList;
})
.doOnError((e) {
print(e.toString());
})
.doOnListen(() =>{})
.doOnDone(() =>{});
}
I/flutter: HomeNewsPage initState
I/flutter: doOnListen
I/flutter: HomeNewsPage--build
I/flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter: The following NoSuchMethodError was thrown building Consumer<HomeProvider>
(dirty, dependencies:
I/flutter: [InheritedProvider<HomeProvider>]):
I/flutter: The getter 'length' was called on null.
I/flutter: Receiver: null
I/flutter: Tried calling: length
I/flutter: When the exception was thrown, this was the stack:
I/flutter: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
I/flutter: #1 _HomeNewsPageBuildState._initView.<anonymous closure> (package:pear_video/app/pages/home/home_news_page.dart:48:34)
I/flutter: #2 Consumer.build (package:provider/src/consumer.dart:47:12)
I/flutter: #3 StatelessElement.build (package:flutter/src/widgets/framework.dart:3885:28)
I/flutter: #4 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:3835:15)
#5..............
#6................
......
答案 0 :(得分:0)
最初,newsList
的值为null
,即当您收到错误消息时,因为null
没有名为length
的属性,因此发生了The getter 'length' was called on null.
错误。 / p>
在调用getHomeNewsList
(可能正在调用一个api)之后,这也需要一些时间,之后它会为List
分配一个_newsList
值。
现在,在newList
返回List
的情况下重新渲染您的UI,_newsList
的属性为length。
一种不会出现初始错误的方法是用空的List
初始化List<DataList> _newsList = [];
。
info1=x;info2=y;.....;info1=.;info2=.;.....