Flutter应用昨日正常运行今日出现黑屏且无报错问题咨询
Flutter App Shows Black Screen with No Errors
Hey there, let's get to the bottom of that black screen issue! After looking through your code, I immediately spotted the root cause.
The Core Problem
In your main() function, you're launching a blank MaterialApp instead of your custom MyApp widget. Since the default empty MaterialApp has no content to render, you end up with a black screen—no errors because the app technically runs, just without any UI!
Quick Fix
Update your main() function to use your MyApp component instead:
void main() { runApp(const MyApp()); }
Full Corrected Code
Here's your code with the fix applied:
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; bool _valuehidra = true; bool _valuelight = true; bool _valuewebasto = true; bool _valueusb = true; bool _valuealter = true; void main() { runApp(const MyApp()); } // <-- Fixed this line class MyApp extends Stateful Widget { const MyApp({Key? key}) : super(key: key); @override _State createState() => _State(); } class _State extends State<MyApp> { bool isSwitched = false; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( margin: const EdgeInsets.fromLTRB(10, 30, 0, 0), child: Image.asset('assets/images/logoisim.png', height: 125.0, fit: BoxFit.cover,), ), ], ), Container( decoration: BoxDecoration( color: Colors.indigoAccent, borderRadius: BorderRadius.circular(30.0), ), width: 350, height: 70, alignment: Alignment.bottomCenter, margin: const EdgeInsets.only(top: 20), child: Row( children: <Widget>[ SizedBox(width: 200, child: Container( margin: const EdgeInsets.only(left: 30), alignment: Alignment.centerLeft, child: const Text('AYDINLATMA', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )), ), ), Transform.scale( scale: 1.3, child: Container( margin: const EdgeInsets.only(left:50), child: CupertinoSwitch( value: _valuelight, onChanged: (value) { setState(() { _valuelight = value; }); }, ), ), ), ], ), ), Container( decoration: BoxDecoration( color: Colors.indigo, borderRadius: BorderRadius.circular(30.0), ), width: 350, height: 70, alignment: Alignment.bottomCenter, margin: const EdgeInsets.only(top: 10), child: Row( children: <Widget>[ SizedBox(width: 200, child: Container( margin: const EdgeInsets.only(left: 30), alignment: Alignment.centerLeft, child: const Text('WEBASTO', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )), ), ), Transform.scale( scale: 1.3, child: Container( margin: const EdgeInsets.only(left:50), child: CupertinoSwitch( value: _valuewebasto, onChanged: (value) { setState(() { _valuewebasto = value; }); }, ), ), ), ], ), ), Container( decoration: BoxDecoration( color: Colors.indigoAccent, borderRadius: BorderRadius.circular(30.0), ), width: 350, height: 70, alignment: Alignment.bottomCenter, margin: const EdgeInsets.only(top: 10), child: Row( children: <Widget>[ SizedBox(width: 200, child: Container( margin: const EdgeInsets.only(left: 30), alignment: Alignment.centerLeft, child: const Text('HİDRAFOR', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )), ), ), Transform.scale( scale: 1.3, child: Container( margin: const EdgeInsets.only(left:50), child: CupertinoSwitch( value: _valuehidra, onChanged: (value) { setState(() { _valuehidra = value; }); }, ), ), ), ], ), ), Container( decoration: BoxDecoration( color: Colors.indigo, borderRadius: BorderRadius.circular(30.0), ), width: 350, height: 70, alignment: Alignment.bottomCenter, margin: const EdgeInsets.only(top: 10), child: Row( children: <Widget>[ SizedBox(width: 200, child: Container( margin: const EdgeInsets.only(left: 30), alignment: Alignment.centerLeft, child: const Text('ALTERNATÖR', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )), ), ), Transform.scale( scale: 1.3, child: Container( margin: const EdgeInsets.only(left:50), child: CupertinoSwitch( value: _valuealter, onChanged: (value) { setState(() { _valuealter = value; }); }, ), ), ), ], ), ), Container( decoration: BoxDecoration( color: Colors.indigoAccent, borderRadius: BorderRadius.circular(30.0), ), width: 350, height: 70, alignment: Alignment.bottomCenter, margin: const EdgeInsets.only(top: 10), child: Row( children: <Widget>[ SizedBox(width: 200, child: Container( margin: const EdgeInsets.only(left: 30), alignment: Alignment.centerLeft, child: const Text('USB (12V)', style: TextStyle(color: Colors.white, fontSize: 25,fontFamily: 'YesevaOne' )), ), ), Transform.scale( scale: 1.3, child: Container( margin: const EdgeInsets.only(left:50), child: CupertinoSwitch( value: _valueusb, onChanged: (value) { setState(() { _valueusb = value; }); }, ), ), ), ], ), ), Container( decoration: BoxDecoration( color: Colors.indigo, borderRadius: BorderRadius.circular(30.0), ), width: 350, height: 90, alignment: Alignment.bottomCenter, margin: const EdgeInsets.only(top: 10), child: Row( children: <Widget>[ Container( margin: const EdgeInsets.only(left: 30), child: Image.asset('assets/images/caravan.png', height: 40.0, fit: BoxFit.cover,), ), Container( margin: const EdgeInsets.only(left: 10), alignment: Alignment.center, child: const Text('KARAVANIM', style: TextStyle(color: Colors.white, fontSize: 31,fontFamily: 'YesevaOne' )), ), Container( margin: const EdgeInsets.only(left: 10), child: Image.asset('assets/images/caravan2.png', height: 40.0, fit: BoxFit.cover,), ), ], ), ), ], ), ), ); } }
Extra Checks (If Issues Persist)
If you still see problems after applying the fix, double-check these:
- Ensure your image assets are properly listed in your
pubspec.yamlunder theflutter/assetssection. - Run
flutter cleanandflutter pub getto clear any cached build issues.
That should get your app running with all your switches and UI elements visible again!
内容的提问来源于stack exchange,提问作者Yusuf Mermer
相关产品推荐
相关产品推荐

