FutureProvider无法访问更新的变量

时间:2020-09-27 08:50:33

标签: flutter future provider

Flutter Provider:

学习FutureProvider需要帮助。 我正在尝试更改onPressed按钮上的文本,仅当我保存文件或进行一些更改并保存后,文本才会更改。

FutureProvider是状态管理器,我必须手动设置其状态吗?

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  return runApp(MaterialApp(
    home: FutureProvider<Txtclass>(
      initialData: Txtclass(mytxt: 'new data'),
      create: (context) => callmyclass(),
      child: Home(),
    ),
  ));
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return HomePage();
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    var cntxt = Provider.of<Txtclass>(context);
    return Scaffold(
        body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(cntxt.mytxt),
          FlatButton(onPressed: () => cntxt.sometext(), child: Text('Press'))
        ],
      ),
    ));
  }
}

class Txtclass with ChangeNotifier {
  String mytxt;
  Txtclass({this.mytxt});
  Future<void> sometext() async {
    await Future.delayed(Duration(seconds: 3), () {
      mytxt = 'my text after 3 seconds';
    });
  }
}

Future<Txtclass> callmyclass() async {
  return Txtclass(mytxt: 'my data');
}

0 个答案:

没有答案