我正在尝试使用Column
来实现Text
:
Column(
children: <Widget>[
Text('data from future function')
],
),
我无法从initState()
获取数据,因为initState()
只是void
如果我直接从函数中获取数据
Text(function)
我知道
功能实例
功能:
Future<double> calculate(int index) async {
LocData _getUser = await getLoc();
double uLat = _getUser.latitude;
double uLng = _getUser.latitude;
double pLat = parks[data].loc.lat;
double pLng = parks[data].loc.lng;
double dis = await Geolocator()
.distanceBetween(uLat , uLng, uLng , pLat );
return dis ;
}
有什么办法可以将这些数据直接从函数中获取到文本wigdet?
答案 0 :(得分:2)
有两种获取未来数据的方法。
选项1:
(带有2个子选项)
<button id='btnUEN' class='k-primary' onclick='getCategoriaByUEN('1','TEST'="" AND="" TEST')'="">TEST AND TEST</button>
选项#2
class MyWidgetState extends State<MyWidget> {
String _someAsyncData;
@override
void initState() {
super.initState();
// opt 1.
aDataFuture.then((val) {
setState(() {
_someAsyncdata = val;
});
});
// opt 2.
_setAsyncData(aDataFuture);
}
void _setAsyncData(Future<String> someFuture) async {
// void + async is considered a "fire and forget" call
// part of opt. 2
_someAsyncData = await someFuture;
// must trigger rebuild with setState
setState((){});
}
Widget build(BuildContext context) {
return _someAsyncData == null ? Container() : Text('$_someAsyncData');
}
}
答案 1 :(得分:1)
这是完整的工作代码。
#[derive(Debug)]
enum Direction {
Up(Point),
Down(Point),
Right(Point),
Left(Point),
}
#[derive(Debug)]
struct Point {
x: u32,
y: u32,
}
#[derive(Debug)]
enum Keys {
Up_key(String),
Down_key(String),
Right_key(String),
Left_key(String),
}
impl Direction {
fn match_direction(&self) -> Keys {
match *self {
Direction::Up(_) => Keys::Up_key(String::from("Up key is pressed")),
Direction::Down(_) => Keys::Down_key(String::from("Down key is pressed")),
Direction::Right(_) => Keys::Right_key(String::from("Right key is pressed")),
Direction::Left(_) => Keys::Left_key(String::from("Left key is pressed")),
}
}
}
impl Keys {
fn destruct(&self) -> &String {
match *self {
Keys::Up_key(ref s) => s,
Keys::Down_key(ref s) => s,
Keys::Left_key(ref s) => s,
Keys::Right_key(ref s) => s,
}
}
}
fn main() {
let test_1 = Direction::Right(Point { x: 1, y: 0 });
let x = test_1.match_direction();
println!("{:#?}", x);
let k = x.destruct();
println!("{}", k);
}