如何在 Flutter 中使用 Provider 正确获取 API
How to correctly fetch APIs using Provider in Flutter
我目前正在处理一个问题,我需要 API 中的一些数据以将其显示到我的小部件中。我遵循了一些 Provider 架构模式,其中你设置了两次:
1- 正在获取数据时
2- 当数据已经被获取时
所以我目前正在处理的问题是,我的小部件抛出以下错误:
setState() or markNeedsBuild() called during build.
我知道这个错误是因为在构建过程中调用了 setState,但是..如何在构建过程中获取我的 api,然后将其显示给我的小部件?这是我的代码:
NewsPage.dart
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
SideBarWidget _sidebar;
@override
void initState() {
Provider.of<HomeViewModel>(context, listen: false)
.fetchUltimaNoticia(context); --> ****Data to get fetched****
_sidebar = const SideBarWidget();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('INICIO'),
centerTitle: true,
automaticallyImplyLeading: false,
leading: Builder(
builder: (context) => IconButton(
icon: const Icon(Icons.menu),
onPressed: () => Scaffold.of(context).openDrawer(),
),
),
),
drawer: _sidebar,
body: FormNoticiaContainer(),
);
}
}
FormContainer()
Widget build(BuildContext context) {
return _crearBodyNoticia(context);
}
Widget _crearBodyNoticia(context) {
final homeVm = Provider.of<HomeViewModel>(context, listen: true);
return homeVm.state == ViewState.Busy
? Center(child: CircularLoading())
: Center(
child: DataToShowWidget()
HomeViewModel.dart
class HomeViewModel extends BaseModel {
////////
//NOTICIAS
////////
NoticiaDB _noticia;
NoticiaDB get noticia => _noticia;
set setNoticia(NoticiaDB noticia) {
_noticia = noticia;
}
Future fetchUltimaNoticia(BuildContext context) async {
setState(ViewState.Busy);
var response = await noticiaProvider.obtenerNoticiaPublicada();
setNoticia = response;
setState(ViewState.Idle);
}
}
基础模型
ViewState _state = ViewState.Idle;
ViewState get state => _state;
void setState(ViewState viewState) {
_state = viewState;
notifyListeners();
}
您可以使用 WidgetsBinding.instance.addPostFrameCallback
详细可以参考https://www.didierboelens.com/faq/week2/
代码片段
@override
void initState(){
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_){
Provider.of<HomeViewModel>(context, listen: false)
.fetchUltimaNoticia(context);
});
}
我目前正在处理一个问题,我需要 API 中的一些数据以将其显示到我的小部件中。我遵循了一些 Provider 架构模式,其中你设置了两次:
1- 正在获取数据时
2- 当数据已经被获取时
所以我目前正在处理的问题是,我的小部件抛出以下错误:
setState() or markNeedsBuild() called during build.
我知道这个错误是因为在构建过程中调用了 setState,但是..如何在构建过程中获取我的 api,然后将其显示给我的小部件?这是我的代码:
NewsPage.dart
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
SideBarWidget _sidebar;
@override
void initState() {
Provider.of<HomeViewModel>(context, listen: false)
.fetchUltimaNoticia(context); --> ****Data to get fetched****
_sidebar = const SideBarWidget();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('INICIO'),
centerTitle: true,
automaticallyImplyLeading: false,
leading: Builder(
builder: (context) => IconButton(
icon: const Icon(Icons.menu),
onPressed: () => Scaffold.of(context).openDrawer(),
),
),
),
drawer: _sidebar,
body: FormNoticiaContainer(),
);
}
}
FormContainer()
Widget build(BuildContext context) {
return _crearBodyNoticia(context);
}
Widget _crearBodyNoticia(context) {
final homeVm = Provider.of<HomeViewModel>(context, listen: true);
return homeVm.state == ViewState.Busy
? Center(child: CircularLoading())
: Center(
child: DataToShowWidget()
HomeViewModel.dart
class HomeViewModel extends BaseModel {
////////
//NOTICIAS
////////
NoticiaDB _noticia;
NoticiaDB get noticia => _noticia;
set setNoticia(NoticiaDB noticia) {
_noticia = noticia;
}
Future fetchUltimaNoticia(BuildContext context) async {
setState(ViewState.Busy);
var response = await noticiaProvider.obtenerNoticiaPublicada();
setNoticia = response;
setState(ViewState.Idle);
}
}
基础模型
ViewState _state = ViewState.Idle;
ViewState get state => _state;
void setState(ViewState viewState) {
_state = viewState;
notifyListeners();
}
您可以使用 WidgetsBinding.instance.addPostFrameCallback
详细可以参考https://www.didierboelens.com/faq/week2/
代码片段
@override
void initState(){
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_){
Provider.of<HomeViewModel>(context, listen: false)
.fetchUltimaNoticia(context);
});
}