onInit函数不会更新视图

时间:2018-06-09 13:20:21

标签: redux dart flutter future

非常新的扑动和飞镖。我有一个小部件(屏幕)加载的情况我想从REST API获取博客文章。所有的抓取和不起作用的东西。我的问题与Redux_flutter StoreConnect onInit函数有关。下面的代码抛出null != post.title异常。所以我看待它的方式。在呈现视图之前调用OnInit。我只是认为当async函数返回结果时视图会更新。如何在Future返回时更新视图。

小工具代码:

import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import '../state.dart';
import '../models/blogpost.dart';


class PostViewPage extends StatelessWidget {
  PostViewPage({Key key, this.postId}) : super(key: key);

  int postId = 8364;

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: AppBar(title: Text("Phela Home"), elevation: 0.0,),
      body: StoreConnector<AppState, BlogPost>(
        converter: (store) => store.state.post,
        onInit: (store) => store.dispatch(GetSinglePostAction(postId)),
        builder: (context, post) => Column(
          children: <Widget>[
            Text(post.title),
          ],
        ),
      )
    );
  }
}

在我的减少中我有:

if(action is GetSinglePostAction)
  {
    final ApiClient apiClient = ApiClient();
    BlogPost blogPost = new BlogPost();
    apiClient.getSingleBlogPost(action.id).then((post) { blogPost = post;});

    print('Action called GETPOST');

    return AppState(
        post: blogPost
    );
  }

0 个答案:

没有答案