Flutter:我想访问setstate的更改后的值,访问它时未显示新的状态值

时间:2020-07-08 13:31:36

标签: flutter dart flutter-dependencies flutter-test

我的代码是:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:testing/test.dart';
import 'package:quiver/time.dart';
import 'dimensions.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

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

class _MyHomePageState extends State<MyHomePage> {

  DateTime _currentdate = new DateTime.now();

  Future<Null> _selectdate(BuildContext context, List date) async{ 
    String sanitizeDateTime(DateTime dateTime) => "${dateTime.month}-${dateTime.day}";

    final DateTime picked =  await showDatePicker(
      context: context,
      initialDate: DateTime(_currentdate.year,_currentdate.month-1),
      firstDate: DateTime(_currentdate.year,_currentdate.month-1),
      lastDate: DateTime(_currentdate.year,_currentdate.month+2),
      selectableDayPredicate: (DateTime val) //=> val.month == 8 ? false:true,
      {
        String santized = sanitizeDateTime(val);
        return !date.contains(santized);
      },
    );

    if(picked!=null&& picked!=_currentdate) {
      setState(() {
        _currentdate = picked;
      });
    }
  }

  List enabledata=[];
  var res;
  demo() async {
    List availablity=[];
    List daysinamonth=[];
    List output=[];
    print('hi');
    res = await checkapt();
    print(res);

    for (var item in res.keys.toList()){
      enablesdates.add(_currentdate.month.toString()+"-"+item);
    }
  }

  @override
  void initState() {
    demo();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Dimensions(context);
    
    String dayonly = new DateFormat.d().format(_currentdate);

    return Scaffold(
      appBar: AppBar(
        title: Text('Date: $dayonly'),
        actions: <Widget>[
          IconButton(
            onPressed: () async {  
              await _selectdate(context,enabledata);
           
              print('$dayonly');
              print(res['$dayonly'];
              for (var item in res['$dayonly']){
                timings.addAll(item);
              }
              print(timings); 
            },
            icon: Icon(Icons.calendar_today),
          ),
        ],
      ),

我正在尝试按用户访问$dayonly作为所选日期,但是它仍然在打印功能中显示_currentdate。我的json res数据的格式为{16: {14: 1}, 26: {19: 1}, 04: {15: 1}, 12: {11: 1}},我希望用户选择一个日期并将新选择的日期保存在$dayonly中。我认为程序在setState工作之前执行,并在$dateonly中获得新的选定日期。例如,currentdate是8,所选日期是12,仍然是当我打印$dateonly时,或者如果我想再次使用res['$dateonly']访问res值,它将显示8。我希望它显示所选内容日期12。

我很陌生,该如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

检查一下。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:testing/test.dart';
import 'package:quiver/time.dart';
import 'dimensions.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

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

class _MyHomePageState extends State<MyHomePage> {

  DateTime _currentdate = new DateTime.now();

  Future<Null> _selectdate(BuildContext context, List date) async{ 
    String sanitizeDateTime(DateTime dateTime) => "${dateTime.month}-${dateTime.day}";

    final DateTime picked =  await showDatePicker(
      context: context,
      initialDate: DateTime(_currentdate.year,_currentdate.month-1),
      firstDate: DateTime(_currentdate.year,_currentdate.month-1),
      lastDate: DateTime(_currentdate.year,_currentdate.month+2),
      selectableDayPredicate: (DateTime val) //=> val.month == 8 ? false:true,
      {
        String santized = sanitizeDateTime(val);
        return !date.contains(santized);
      },
    );

    if(picked!=null&& picked!=_currentdate) {
      setState(() {
        _currentdate = picked;
      });
    }
  }

  List enabledata=[];
  var res;
  demo() async {
    List availablity=[];
    List daysinamonth=[];
    List output=[];
    print('hi');
    res = await checkapt();
    print(res);

    for (var item in res.keys.toList()){
      enablesdates.add(_currentdate.month.toString()+"-"+item);
    }
  }

  @override
  void initState() {
    demo();
    super.initState();
  }
  String dayonly = new DateFormat.d().format(_currentdate);
  @override
  Widget build(BuildContext context) {
    Dimensions(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Date: $dayonly'),
        actions: <Widget>[
          IconButton(
            onPressed: () async {  
              await _selectdate(context,enabledata);
           
              print('$dayonly');
              print(res['$dayonly'];
              for (var item in res['$dayonly']){
                timings.addAll(item);
              }
              print(timings); 
            },
            icon: Icon(Icons.calendar_today),
          ),
        ],
      ),