希望一切都好。
我是 flutter 的新手。
我想在加载时调用一个方法?
我累了
void initState() {
super.initState();
log('Load Event');
}
但它不起作用?
任何机会
这是我的完整代码
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'dart:developer';
import 'package:flutter/scheduler.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: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Position _currentPosition;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Trex Partner Finder hh"),
),
body: Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/images/TrexIcon.png"),
fit: BoxFit.scaleDown,
),
),
),
floatingActionButton:
FloatingActionButton.extended(
onPressed: () {
_getCurrentLocation();
},
label: Text(" Find Partner Alooo "),
),
);
}
void initState() {
super.initState();
log('h');
}
_getCurrentLocation() {
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
}).catchError((e) {
print(e);
});
log(_currentPosition.longitude.toString());
log(_currentPosition.latitude.toString());
}
}
最佳答案
将您的方法放入 initState
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'dart:developer';
import 'package:flutter/scheduler.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: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
_getCurrentLocation();
}
Position _currentPosition;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Trex Partner Finder hh"),
),
body: Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/images/TrexIcon.png"),
fit: BoxFit.scaleDown,
),
),
),
floatingActionButton:
FloatingActionButton.extended(
onPressed: () {},
label: Text(" Find Partner Alooo "),
),
);
}
_getCurrentLocation() {
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
}).catchError((e) {
print(e);
});
log(_currentPosition.longitude.toString());
log(_currentPosition.latitude.toString());
}
}
更多信息initState
this
关于flutter - 如何在 flutter 中调用加载方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60004789/