我正在使用 flutter_provider 进行状态管理。我想从 Api 加载页面(statefulwidget)上的一些项目。我在页面开始时显示一个加载程序,并希望在获取项目后显示它们。
播放列表.dart -
class Playlist extends StatefulWidget {
@override
_PlaylistState createState() => _PlaylistState();
}
class _PlaylistState extends State<Playlist> {
var videosState;
@override
void initState() {
super.initState();
videosState = Provider.of<VideosProvider>(context);
videosState.fetchVideosList();
}
@override
Widget build(BuildContext context) {
var videos = videosState.playlist;
return Scaffold(
appBar: AppBar(
title: Text('My Videos'),
),
body: RefreshIndicator(
child: Container(
width: double.infinity,
height: double.infinity,
child: videos.length
? ListView.builder(
itemBuilder: (BuildContext context, index) {
return _videoListItem(context, index, videos, videosState);
},
itemCount: videos.length,
)
: Center( …Run Code Online (Sandbox Code Playgroud) 我正在关注关于flutter的udemy课程(https://www.udemy.com/learn-flutter-dart-to-build-ios-android-apps),并使用scoped_model进行状态管理。我想从产品列表中编辑产品。为此,我将产品 ID 设置为标志 - 范围模型中的 selectedProductId。提交时,我想导航离开并将此 selectedProductId 设置为 null。
//method to edit product
editProduct(productId, newValues);
Navigator.pushReplacementNamed(context, '/productsPage').then((_){
debugger();
setSelectedProductId(null);
}).catchError((err){print(err);});
Run Code Online (Sandbox Code Playgroud)
调试器永远不会被命中。并且也没有错误。
我尝试使用异步等待,但仍然没有用。
editProduct(editableProductId, _formData);
await Navigator.pushReplacementNamed(context, '/productsPage').catchError((err){print(err);});
debugger();
setSelectedProductId(null);
Run Code Online (Sandbox Code Playgroud)
页面被导航到给定的路线,但后面的行永远不会执行。我什至尝试使用pushNamed而不是pushReplacementNamed.
我知道pushReplacementNamed返回一个未来,所以必须调用 .then(),但似乎没有发生,我错过了什么吗?还有其他我应该了解的因素或情况吗?