如何更新并再次获取FutureProvider数据?

时间:2020-05-23 14:59:04

标签: flutter flutter-provider

我想知道当我使用FutureProvider时如何获取更新的数据。

单击按钮后如何再次获取并显示更新的数据?

它有好的方法吗?还是我应该放弃使用FutureProvider?

    class MyApp extends StatelessWidget {

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: FutureProvider(
              create: (_) async => TestDao().findAll(),
              child: MyHomePage(title: 'Flutter Demo Home Page')),
        );
      }
    }

    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);

      final String title;

      @override
      _MyHomePageState createState() => _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {

      @override
      Widget build(BuildContext context) {
        var testData = Provider.of<List<String>>(context);
        return Scaffold(
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              setState(() {
                TestDao().insert('test');
              });
            },
            child: Icon(Icons.add),
          ),
          body: Text('$testData'),
        );
      }
    }

0 个答案:

没有答案