【问题标题】:flutter future change value when inside future builder在未来构建器内部时颤动未来变化值
【发布时间】:2021-01-25 11:42:16
【问题描述】:

你好我是新来的颤振和构建我的第一个应用程序,我可以根据值更改在构建器中更改未来函数中的值,然后将其重置为应用程序终止时的方式吗? Archive.dart


class _ArchiveState extends State<Archive> {
  Future<List<YearsMain>> downloadJSONMain() async {
    String year;
    SharedPreferences pref = await SharedPreferences.getInstance();
    year = pref.getString('year');
    final jsonEndpoint = "http://msc-mu.com/api_verfication.php";

    final response = await http.post(jsonEndpoint, body: {
      'flag': 'selectmainsubjects',
      'year': year,
    });

    if (response.statusCode == 200) {
      List mainSubject = json.decode(response.body);
      return mainSubject.map((mains) => new YearsMain.fromJson(mains)).toList();
    } else
      throw Exception(
          'We were not able to successfully download the Main Subjects.');
  }
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: new FutureBuilder<List<YearsMain>>(
          future: downloadJSONMain(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              List<YearsMain> mains = snapshot.data;
              return ListViewMain(mains);
            } else if (snapshot.hasError) {
              return Text('${snapshot.error}');
            }
            return CircularProgressIndicator();
          },
        ),
      ),
    );
  }
}

我想实现一个下拉列表来更改未来函数中的“年份”值,当应用程序关闭时,我希望该值恢复原来的状态,有没有办法做到这一点或通过放置另一个字符串我其实不知道,有什么帮助吗?

【问题讨论】:

    标签: mysql flutter flutter-futurebuilder


    【解决方案1】:

    您可以使用DropdownButton 允许用户更改年份。但在此之前,将您未来的逻辑移至 initState。使用 Dropdown 的“onChanged”回调在 setState 内更新您的 sharedPreferences 实例和未来。这是一个例子:

    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      Future future;
      String selectedYear = "2020";
    
      @override
      void initState() {
        future = downloadJSONMain(); // or just set the "future" variable inside the "downloadJSONMain function
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return DropdownButton<String>(
          value: dropdownValue,
          icon: Icon(Icons.arrow_downward),
          iconSize: 24,
          elevation: 16,
          style: TextStyle(color: Colors.deepPurple),
          underline: Container(
            height: 2,
            color: Colors.deepPurpleAccent,
          ),
          onChanged: (String newValue) {
            setState(() {
              selectedYear = newValue;
              // update the future once user select a new year
              future = downloadJSONMain();
              // also save it to the sharedPrefeences here
              // ...
    
            });
          },
          items: <String>["2017", "2018", "2019", "2020"]
              .map<DropdownMenuItem<String>>((String value) {
            return DropdownMenuItem<String>(
              value: value,
              child: Text(value),
            );
          }).toList(),
        );
      }
    }
    

    【讨论】:

    • 应用关闭后如何恢复原值?
    • 你的意思是“原始”的默认值吗?如果是这样,它将使用您在状态中设置的默认值:String selectedYear = "2020"。如果您想使用用户之前设置的值,您已经在使用这种方式与该行:year = pref.getString('year');。正如我在代码中展示的那样,您应该使用 onChanged 将其保存到首选项中。
    • 我可以和你谈谈 discord 吗?
    • 有一个值在该页面之前已经保存在共享首选项中,有什么办法可以找回它吗?应用关闭后?
    猜你喜欢
    • 2022-09-30
    • 2020-08-29
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    • 2021-06-23
    • 2021-10-07
    • 2021-10-10
    • 2023-01-18
    相关资源
    最近更新 更多