希望一切都好。
我是扑的新手。
我想在加载时调用一个方法?
我累了
void initState() {
super.initState();
log('Load Event');
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用?
任何机会
这是我的完整代码
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());
}
}
Run Code Online (Sandbox Code Playgroud)
把你的数学放进去 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());
}
}
Run Code Online (Sandbox Code Playgroud)
有关这方面的更多信息initState
小智 0
@override
void initState() {
super.initState();
print('hi there');
// Or call your function here
}
Run Code Online (Sandbox Code Playgroud)
应该管用
| 归档时间: |
|
| 查看次数: |
3352 次 |
| 最近记录: |