Home > Mobile >  Unable import package
Unable import package

Time:06-20

I'm trying to import the Mainpage as package like I always do but I can't. I don't understand why bcs it works all the time. This is what it said when I click view problem

The method 'MainPage' isn't defined for the type 'MyApp'. Try correcting the name to the name of an existing method, or defining a method named 'MainPage'.dartundefined_method

This is the code

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized;
  await Firebase.initializeApp();
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MainPage (),
    );
   }
 }

I also try with other words like LoginPage, but it still don't work.

CodePudding user response:

I think you don't create a dart file called ‍MainPage. You must first create a MainPage.

You create a dart file named MainPage, then import that file to the main page.

CodePudding user response:

You probably did not create a HomePage widget. You can fix it by creating it.

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MainPage(), //No error here
    );
  }
}

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

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Center(child: Text("Hello World")));
  }
}

CodePudding user response:

Have you created the MainPage on the very same file? If not, you are missing the MainPage file import at the start, something like:

import 'package:your_app/maybe_a_folder/your_file_that_contains_main_page.dart';

CodePudding user response:

home: MainPage(), instead of home: MainPage (),

remove space between MainPage and function brackets ()

  • Related