简体   繁体   中英

"The method 'SplashScreen' isn't defined for the type 'MyApp'.". I have tried a lot of things and nothing. Any solution?

main.dart:

import 'package:flutter/material.dart' show BuildContext, Colors, MaterialApp, StatelessWidget, ThemeData, VisualDensity, Widget, runApp;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        // Application name
        title: 'UranteX',
        theme: ThemeData(
          primarySwatch: Colors.red,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: SplashScreen());
  }

splashScreen.dart(2nd page)

import 'package:flutter/material.dart' show BuildContext, Center, Column, Container, CrossAxisAlignment, Image, MainAxisAlignment, Scaffold, SizedBox, State, StatefulWidget, Widget;

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Center(
          child: Column(crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [
            SizedBox(
              height: 300.0,
              child: Image.asset('Images/logo.png'),
            )
          ]),
        ),
      ),
    );
  }
}

///The code isn't working and its saying that"The method 'SplashScreen' isn't defined for the type 'MyApp'". I know the solution probably is on the "Container" but I can't solve it.

Solution: You have to import splashScreen.dart file into your main.dart Reason of problem: You are calling SplashScreen class which is in another file so how main.dart file will get to know that what is SplashScreen there So you need to import splashscreen.dart file so that main.dart will know that SplashScreen is a class from splashScreen.dart file.

If you still have doubt then you can ask if you satisfied then give this answer an upvote so that more will learn this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM