8

Hope everythign is fine.

I am new to flutter.

I want to call a method on load ?

I tired

void initState() {
    super.initState();
    log('Load Event');
  }

But it is not working ?

Any chance

Here is my full code

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());
  }
}
5
  • try adding the overide annotation like @override void initState() { print('h'); super.initState(); } Commented Jan 31, 2020 at 13:24
  • @TinusJackson not owrking Commented Jan 31, 2020 at 13:26
  • dartpad.dev/b6409e10de32b280b8938aa75364fa7b minimal product with just the initstate and its working Commented Jan 31, 2020 at 13:33
  • 1
    @TinusJackson Can I detect if position is changed Commented Jan 31, 2020 at 17:10
  • 1
    @TinusJackson How can Listen if application is closed Commented Feb 2, 2020 at 10:55

2 Answers 2

10

Put your mathod in 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());
  }
}

for more info about initState this

Sign up to request clarification or add additional context in comments.

3 Comments

@ A R it worked but how can I get the location of device on load
@ A R How can Listen if application is closed
You have to Implement Android LifeCycle
0
    @override
    void initState() {
      super.initState();
      print('hi there');

      // Or call your function here
    }

should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.