flutter - 如何在 flutter 中调用加载方法?

标签 flutter

希望一切都好。

我是 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/

相关文章:

flutter - 如何将文本添加到 TextField

flutter - 在另一个页面编辑数据后更新 Flutter 页面的惯用方法?

flutter - Flutter-显示框/菜单以及TextField上的列表更改事件

Flutter:使用 try catch 保留错误的 Stacktrace

flutter - 为什么列表长度为0?为什么不添加数据?

flutter 包在包中找不到类

firebase - flutter Isolate 中的共享偏好

dart - 文本中的星期几、月份和天数

flutter - 如何解决flutter中截断的文本

flutter - 难以将商品添加到购物车 - Flutter