Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 是否有一种方法可以使用Dart中的未来值来填充文本字段?_Flutter_Dart_Async Await_Futuretask - Fatal编程技术网

Flutter 是否有一种方法可以使用Dart中的未来值来填充文本字段?

Flutter 是否有一种方法可以使用Dart中的未来值来填充文本字段?,flutter,dart,async-await,futuretask,Flutter,Dart,Async Await,Futuretask,我一直在用颤振学习Dart,并在为实践创建天气应用程序,我成功地创建了UI,我使用了Dark Sky天气API,因为它有7天的天气预报,但我没有走解码json和模型的漫长道路,而是使用了。 下面是API的代码 Future<Null> getWeather() async { Location location = Location(); await location.getCurrentPosition(); var darksky = DarkSkyWeather( MY_A

我一直在用颤振学习Dart,并在为实践创建天气应用程序,我成功地创建了UI,我使用了Dark Sky天气API,因为它有7天的天气预报,但我没有走解码json和模型的漫长道路,而是使用了。 下面是API的代码

Future<Null> getWeather() async {
Location location = Location();
await location.getCurrentPosition();
var darksky = DarkSkyWeather(
  MY_API_KEY,
  units: Units.SI,
  language: Language.English,
);
var forecast = await darksky.getForecast(
    location.latitude, location.longitude, excludes: [
    Exclude.Hourly,
    Exclude.Minutely,
    Exclude.Alerts,
    Exclude.Flags
    ]);
print(forecast.currently.temperature.round());
print(forecast.daily.data[0].temperatureMax);
 }
Future getWeather()异步{
位置=位置();
等待位置。getCurrentPosition();
var darksky=DarkSkyWeather(
我的API密钥,
单位:units.SI,
语言:语言,英语,
);
var forecast=等待darksky.getForecast(
location.latitude、location.longitude,不包括:[
不包括。每小时,
排除。每分钟,
排除。警报,
排除。标志
]);
打印(forecast.Current.temperature.round());
打印(forecast.daily.data[0].temperatureMax);
}
我想在函数之外使用预测变量,并填充文本字段,例如包中的湿度、温度和其他数据。我如何访问它?任何帮助都将不胜感激。
谢谢

看看这段代码

class _MyHomePageState extends State<MyHomePage> {

  String _data; // This holds the data to be shown

  @override
  void initState() {
    _data = "Press the button"; // This is the initial data // Set it in initState because you are using a stateful widget
    super.initState();
  }

  // Call this to set the new data
  void setData() {
    setState(() { // Remember to use setState() else the changes won't appear
      _data = 'Hello World';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Message',
            ),
            Text(
              '$_data', // See how the data is fed into this text widget
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: setData, // When I press the button, the new data is set
        child: Icon(Icons.send),
      ), 
    );
  }
}
class\u MyHomePageState扩展状态{
String _data;//它保存要显示的数据
@凌驾
void initState(){
_data=“Press the button”;//这是初始数据//将其设置为initState,因为您使用的是有状态小部件
super.initState();
}
//调用此函数以设置新数据
void setData(){
setState((){//记住使用setState(),否则更改不会出现
_数据='你好世界';
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
"讯息",,
),
正文(
“$\u data”,//查看数据是如何输入到此文本小部件的
样式:Theme.of(context).textTheme.display1,
),
],
),
),
浮动操作按钮:浮动操作按钮(
onPressed:setData,//当我按下按钮时,新数据被设置
子:图标(Icons.send),
), 
);
}
}
输出:

最初:

按下按钮后:


在代码中执行相同的操作

// Your code modified a bit
// Make sure this is a stateful widget because you are changing values
double _tempNow; // Declare two vars inside the class
double _tempMax;

// Your initState
@override
void initState() {
    _tempNow = 0; // Initial values
    _tempMax = 0;
    super.initState();
}

// Build method
@override
Widget build(BuildContext context) {
    // Your layout Code.....
         Text(
              'Current: $_tempNow' // Here you set the tempNow
         ),
         ......
         Text(
              'Maximum: $_tempMax' // and tempMax in the text widgets
         ),
    // End
}

Future<Null> getWeather() async {
    Location location = Location();
    await location.getCurrentPosition();
    var darksky = DarkSkyWeather(
        "8810b3633934b8c1975c51a2311dc1d0",
        units: Units.SI,
        language: Language.English,
    );
    var forecast = await darksky.getForecast(
        location.latitude, location.longitude, excludes: [
        Exclude.Hourly,
        Exclude.Minutely,
        Exclude.Alerts,
        Exclude.Flags
    ]);

    // Change the values here.
    // After the data is fetched, The changes will be made automatically
    setState(() {
        _tempNow = forecast.currently.temperature.round();
        _tempMax = forecast.daily.data[0].temperatureMax;
    });
 }
//您的代码修改了一点
//确保这是一个有状态的小部件,因为您正在更改值
双倍_tempNow;//在类中声明两个变量
双温度最大值;
//你的国家
@凌驾
void initState(){
_tempNow=0;//初始值
_tempMax=0;
super.initState();
}
//构建方法
@凌驾
小部件构建(构建上下文){
//您的布局代码。。。。。
正文(
'Current:$\u tempNow'//您可以在这里设置tempNow
),
......
正文(
'Maximum:$\u tempMax'//和文本小部件中的tempMax
),
//结束
}
Future getWeather()异步{
位置=位置();
等待位置。getCurrentPosition();
var darksky=DarkSkyWeather(
“8810B3633934B8C1975C51A311DC1D0”,
单位:units.SI,
语言:语言,英语,
);
var forecast=等待darksky.getForecast(
location.latitude、location.longitude,不包括:[
不包括。每小时,
排除。每分钟,
排除。警报,
排除。标志
]);
//在此处更改值。
//获取数据后,将自动进行更改
设置状态(){
_tempNow=forecast.current.temperature.round();
_tempMax=forecast.daily.data[0]。temperatureMax;
});
}
同样,您也可以使用文本字段。在这种情况下,您将需要在那里使用和设置文本,但在您的情况下,您似乎需要只读文本来向用户显示信息,因此一个简单的文本小部件就足够了