我是新来扑,我是学习应用程序已经启动画面,然后进入新的页面。我添加了一个依赖启动画面:我的项目。由于我是新的我不如何实现启动画面,当我搜索我将依赖于项目的解决方案。
当我试图运行我的应用程序,我得到了下面的错误。
I/flutter (28504): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter (28504): The following assertion was thrown building
SplashScreen(state: _SplashScreenState#6edd2):
I/flutter (28504): MediaQuery.of() called with a context that does not
contain a MediaQuery.
I/flutter (28504): No MediaQuery ancestor could be found starting from the
context that was passed to MediaQuery.of().
I/flutter (28504): This can happen because you do not have a WidgetsApp or
MaterialApp widget (those widgets introduce
I/flutter (28504): a MediaQuery), or it can happen if the context you use
comes from a widget above those widgets.
I/flutter (28504): The context used was:
I/flutter (28504): Scaffold(dirty, state: ScaffoldState#a8879(lifecycle
state: initialized, tickers: tracking 1
I/flutter (28504): ticker))
这是我的pubspec.yaml
name: bmi_calculator
description: A flutter application for knowing you BMI.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
splashscreen:
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
我main.dart
import 'package:flutter/material.dart';
import 'package:splashscreen/splashscreen.dart';
import 'package:bmi_calculator/BmiPage.dart';
main(){
runApp(BmiCalculator());
}
class BmiCalculator extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return BmiCalculatorstate();
}
}
class BmiCalculatorstate extends State<BmiCalculator>{
@override
Widget build(BuildContext context) {
return new SplashScreen(
seconds: 10,
navigateAfterSeconds: new BmiPage(),
title: Text("Welcome to BMI CALCULATOR",
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 10.0,
color: Colors.white
),
),
backgroundColor: Colors.red,
);
}
}
这是我的BmiPage.dart
import 'package:flutter/material.dart';
class BmiPage extends StatefulWidget{
@override
State<StatefulWidget> createState() {
return BmiPageState();
}
}
class BmiPageState extends State<BmiPage>{
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: new AppBar(
title: Text(
'BMI CALCULATOR',
style: new TextStyle(
color: Colors.white
),
),
backgroundColor: Colors.red,
),
),
);
}
}
为什么会出现这个错误,我怎么能解决这个问题?
提前致谢。
实际发生的错误,因为你没有一个有效的上下文由于众所周知的框架问题,可能被固定在以后的更新,你确实需要在runApp()
方法使用一个无状态的Widget,然后在无状态回报您BMICaculator()
build
方法。
runApp(MyApp());
class MyApp extends StatelessWidget {
void build(BuildContext context)=> MaterialApp(home: BMICalculator());
}
此外,你实际上并不需要一个插件来闪屏添加到您的应用程序。有两种类型的“闪屏”的:
我建议你阅读this article解释如何一步一步的闪屏添加到您的应用程序(情况1)