Ankit Kumar Maurya
Ankit Kumar Maurya

Reputation: 1217

Navigation problem using GetX package in flutter

I am using the GetX package for the navigation purpose in flutter project but it is not working for me. It is showing some problems in debug console also - 'Unexpected format, you can only use widgets and widget functions here'.

Below is my main.dart file-

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jarvis/networking/login.dart';

void main() {
  runApp(const MyApp());
  //Get.to(Login());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData('Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _loginScreen(){
    Get.to(Login());
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
      //Get.to(Login());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            TextButton(onPressed: _loginScreen, child: const Text("Press here to go to next page", style: TextStyle(fontSize: 20),)),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Whenever the user will click on the Text button, it is supposed that the login screen will appear but there is no change in the UI. Below is my login_screen.dart file -


import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart';

class Login extends StatelessWidget {
  const Login({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Material(
      child: GetMaterialApp(
        title: "Login Screen",
        home: GetOtp(),
      ),
    );
  }
}

class GetOtp extends StatefulWidget {
  const GetOtp({ Key? key }) : super(key: key);

  @override
  _GetOtpState createState() => _GetOtpState();
}

class _GetOtpState extends State<GetOtp> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Login"),
      ),
      body: const Center(
        child: Text("hi here ankit", style: TextStyle(color: Colors.red, fontSize: 20),),
      ),
    );
  }
}

I tried but unable to find the mistake.

Upvotes: 0

Views: 4048

Answers (2)

Akbar Ansari
Akbar Ansari

Reputation: 1

I also face the same problem, by below changes my error is gone,

In you main.dart file, change mian function to this:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

and your changeScreen function should look likes this:

    changeScreen() {
    Future.delayed(const Duration(seconds: 3), () {
      Get.offAll(const LoginScreen());
    });
  }

Upvotes: 0

Bhumika Patel
Bhumika Patel

Reputation: 96

In your main.dart file, you can directly add this:

void main() { runApp(const Login()); }

Upvotes: 1

Related Questions