我是扑朔迷离的新手,而且我正在跟踪此Udemy课程,但是当我在Windows计算机上的Android仿真器上运行应用程序时,它会在整个屏幕上显示按钮
简单的应用程序,我只是试图从用户那里获取数据,以将其存储在s文件中,然后在控制台中将其打印出来
预期结果
我的代码:-
main.dart
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
void main() async{
var data = await readData();
if(data != null){
print(data);
}
runApp(
MaterialApp(
title: "IO",
home: Home(),
)
);
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
var _enterDataField = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Read/Write"),
centerTitle: true,
backgroundColor: Colors.greenAccent,
),
body: new Container(
padding: EdgeInsets.all(13.4),
alignment: Alignment.topCenter,
child: ListTile(
title: TextField(
controller: _enterDataField,
decoration: InputDecoration(
labelText: "Input Something"
),
),
subtitle: FlatButton(
color: Colors.redAccent,
onPressed: (){
print("Flat button");
writeData(_enterDataField.text);
},
child: new Column(
children: <Widget>[
Text("Save Data"),
Padding(padding: EdgeInsets.all(14.5)),
Text("Save data goes all"),
],
),
),
),
),
);
}
}
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path; // /home/directory/
}
Future<File> get _localFile async {
final path = await _localPath;
return File("$path/data.txt"); // /home/directory/data.txt
}
Future<File> writeData(String message) async {
final file = await _localFile;
return file.writeAsString("$message");
}
Future<String> readData() async{
try{
final file = await _localFile;
String data = await file.readAsString();
return data;
}catch(e){
return "Nothing has been saved yet!";
}
}
pubspec.yaml
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
path_provider: ^1.1.0
运行过程中出现错误:
Launching lib\main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Gradle task 'assembleDebug'...
Built build\app\outputs\apk\debug\app-debug.apk.
I/flutter ( 4904): Nothing has been saved yet!
I/Choreographer( 4904): Skipped 105 frames! The application may be doing too much work on its main thread.
Syncing files to device Android SDK built for x86...
D/EGL_emulation( 4904): eglMakeCurrent: 0xe7085900: ver 3 1 (tinfo 0xe70839b0)
I/flutter ( 4904): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4904): The following assertion was thrown during performLayout():
I/flutter ( 4904): not implemented
I/flutter ( 4904): 'package:flutter/src/material/input_decorator.dart': Failed assertion: line 977 pos 12: 'false'
I/flutter ( 4904):
I/flutter ( 4904): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 4904): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 4904): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 4904): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 4904):
I/flutter ( 4904): When the exception was thrown, this was the stack:
I/flutter ( 4904): #2 _RenderDecoration.computeDistanceToActualBaseline (package:flutter/src/material/input_decorator.dart:977:12)
I/flutter ( 4904): #3 RenderBox.getDistanceToActualBaseline.<anonymous closure> (package:flutter/src/rendering/box.dart:1672:50)
I/flutter ( 4904): #4 __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.putIfAbsent (dart:collection/runtime/libcompact_hash.dart:281:23)
I/flutter ( 4904): #5 RenderBox.getDistanceToActualBaseline (package:flutter/src/rendering/box.dart:1672:22)
I/flutter ( 4904): #6 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.computeDistanceToActualBaseline (package:flutter/src/rendering/proxy_box.dart:97:20)
I/flutter ( 4904): #7 RenderBox.getDistanceToActualBaseline.<anonymous closure> (package:flutter/src/rendering/box.dart:1672:50)
I/flutter ( 4904): #8 __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.putIfAbsent (dart:collection/runtime/libcompact_hash.dart:281:23)
I/flutter ( 4904): #9 RenderBox.getDistanceToActualBaseline (package:flutter/src/rendering/box.dart:1672:22)
I/flutter ( 4904): #10 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.computeDistanceToActualBaseline (package:flutter/src/rendering/proxy_box.dart:97:20)
I/flutter ( 4904): #11 RenderBox.getDistanceToActualBaseline.<anonymous closure> (package:flutter/src/rendering/box.dart:1672:50)
I/flutter ( 4904): #12 __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.putIfAbsent (dart:collection/runtime/libcompact_hash.dart:281:23)
I/flutter ( 4904): #13 RenderBox.getDistanceToActualBaseline (package:flutter/src/rendering/box.dart:1672:22)
I/flutter ( 4904): #14 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.computeDistanceToActualBaseline (package:flutter/src/rendering/proxy_box.dart:97:20)
I/flutter ( 4904): #15 RenderBox.getDistanceToActualBaseline.<anonymous closure> (package:flutter/src/rendering/box.dart:1672:50)
I/flutter ( 4904): #16 __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.putIfAbsent (dart:collection/runtime/libcompact_hash.dart:281:23)
I/flutter ( 4904): #17 RenderBox.getDistanceToActualBaseline (package:flutter/src/rendering/box.dart:1672:22)
I/flutter ( 4904): #18 RenderBox.getDistanceToBaseline (package:flutter/src/rendering/box.dart:1655:27)
I/flutter ( 4904): #19 _RenderListTile._boxBaseline (package:flutter/src/material/list_tile.dart:886:16)
I/flutter ( 4904): #20 _RenderListTile.performLayout (package:flutter/src/material/list_tile.dart:945:32)
I/flutter ( 4904): #21 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #22 RenderPadding.performLayout (package:flutter/src/rendering/shifted_box.dart:199:11)
I/flutter ( 4904): #23 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #24 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #25 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #26 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #27 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #28 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #29 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #30 RenderPositionedBox.performLayout (package:flutter/src/rendering/shifted_box.dart:385:13)
I/flutter ( 4904): #31 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #32 RenderPadding.performLayout (package:flutter/src/rendering/shifted_box.dart:199:11)
I/flutter ( 4904): #33 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #34 MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:142:11)
I/flutter ( 4904): #35 _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:339:7)
I/flutter ( 4904): #36 MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:212:7)
I/flutter ( 4904): #37 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:356:14)
I/flutter ( 4904): #38 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #39 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #40 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #41 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #42 _RenderCustomClip.performLayout (package:flutter/src/rendering/proxy_box.dart:1203:11)
I/flutter ( 4904): #43 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #44 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #45 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #46 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #47 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #48 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #49 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #50 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #51 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #52 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #53 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #54 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #55 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #56 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #57 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #58 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #59 RenderOffstage.performLayout (package:flutter/src/rendering/proxy_box.dart:2900:13)
I/flutter ( 4904): #60 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #61 RenderStack.performLayout (package:flutter/src/rendering/stack.dart:510:15)
I/flutter ( 4904): #62 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #63 __RenderTheatre&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #64 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #65 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #66 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #67 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #68 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #69 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #70 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #71 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #72 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #73 _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:104:13)
I/flutter ( 4904): #74 RenderObject.layout (package:flutter/src/rendering/object.dart:1634:7)
I/flutter ( 4904): #75 RenderView.performLayout (package:flutter/src/rendering/view.dart:147:13)
I/flutter ( 4904): #76 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1509:7)
I/flutter ( 4904): #77 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:768:18)
I/flutter ( 4904): #78 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:281:19)
I/flutter ( 4904): #79 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:686:13)
I/flutter ( 4904): #80 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:219:5)
I/flutter ( 4904): #81 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 4904): #82 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 4904): #83 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:751:7)
I/flutter ( 4904): #85 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
I/flutter ( 4904): #86 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
I/flutter ( 4904): #87 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter ( 4904): (elided 3 frames from class _AssertionError and package dart:async)
I/flutter ( 4904):
I/flutter ( 4904): The following RenderObject was being processed when the exception was fired:
I/flutter ( 4904): _RenderListTile#c955b relayoutBoundary=up7 NEEDS-LAYOUT NEEDS-PAINT
I/flutter ( 4904): creator: _ListTile ← MediaQuery ← Padding ← SafeArea ← Semantics ← Listener ← _GestureSemantics ←
I/flutter ( 4904): RawGestureDetector ← GestureDetector ← InkWell ← ListTile ← Align ← ⋯
I/flutter ( 4904): parentData: offset=Offset(0.0, 0.0) (can use size)
I/flutter ( 4904): constraints: BoxConstraints(0.0<=w<=352.6, 0.0<=h<=668.1)
I/flutter ( 4904): size: MISSING
I/flutter ( 4904): This RenderObject had the following descendants (showing up to depth 5):
I/flutter ( 4904): RenderSemanticsAnnotations#30cbc relayoutBoundary=up8 NEEDS-PAINT
I/flutter ( 4904): RenderIgnorePointer#d2ac5 relayoutBoundary=up9 NEEDS-PAINT
I/flutter ( 4904): RenderPointerListener#95c88 relayoutBoundary=up10 NEEDS-PAINT
I/flutter ( 4904): _RenderDecoration#d39f5 relayoutBoundary=up11 NEEDS-PAINT
I/flutter ( 4904): RenderRepaintBoundary#7e8a4 relayoutBoundary=up12 NEEDS-PAINT
I/flutter ( 4904): RenderTransform#6a1d0 relayoutBoundary=up12 NEEDS-PAINT
I/flutter ( 4904): RenderConstrainedBox#bd9be relayoutBoundary=up12 NEEDS-PAINT
I/flutter ( 4904): RenderCustomPaint#862dd NEEDS-PAINT
I/flutter ( 4904): RenderSemanticsAnnotations#103e3 relayoutBoundary=up8 NEEDS-PAINT
I/flutter ( 4904): _RenderInputPadding#ab3b4 relayoutBoundary=up9 NEEDS-PAINT
I/flutter ( 4904): RenderConstrainedBox#39a4f relayoutBoundary=up10 NEEDS-PAINT
I/flutter ( 4904): RenderPhysicalShape#32f8b relayoutBoundary=up11 NEEDS-PAINT
I/flutter ( 4904): RenderCustomPaint#5950d relayoutBoundary=up12 NEEDS-PAINT
I/flutter ( 4904): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 4904): Another exception was thrown: RenderBox was not laid out: _RenderListTile#c955b relayoutBoundary=up7 NEEDS-PAINT
I/flutter ( 4904): Another exception was thrown: RenderBox was not laid out: RenderPadding#a1895 relayoutBoundary=up6 NEEDS-PAINT
I/flutter ( 4904): Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#9585a relayoutBoundary=up5 NEEDS-PAINT
I/flutter ( 4904): Another exception was thrown: RenderBox was not laid out: RenderPointerListener#867ef relayoutBoundary=up4 NEEDS-PAINT
I/flutter ( 4904): Another exception was thrown: 'package:flutter/src/rendering/shifted_box.dart': Failed assertion: line 314 pos 12: 'child.hasSize': is not true.
D/ ( 4904): HostConnection::get() New Host Connection established 0xce8870c0, tid 4923
D/EGL_emulation( 4904): eglMakeCurrent: 0xe569c960: ver 3 1 (tinfo 0xe447f500)
答案 0 :(得分:0)
设置列min的mainAxisSize
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("Save Data"),
Padding(padding: EdgeInsets.all(14.5)),
Text("Save data goes all"),
],
),
答案 1 :(得分:0)
使用它。
Widget _myWidget() {
return Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextField(decoration: InputDecoration(hintText: "Write Something")),
Container(
color: Colors.red,
width: 200,
height: 100,
child: Column(children: <Widget>[
Text("Save Data"),
Spacer(),
Text("Save Data goes here"),
],),
),
],
),
);
}
答案 2 :(得分:0)
为了工作,我不得不将代码编辑成这样
FlatButton(
color: Colors.redAccent,
onPressed: (){
print("Flat button ${_enterDataField.text}");
writeData(_enterDataField.text.toString());
},
child: new Column(
children: <Widget>[
Text("Save Data"),
Padding(padding: EdgeInsets.all(14.5)),
FutureBuilder(
future: readData(),
builder: (BuildContext context, AsyncSnapshot<String> data){
if(data.hasData != null){
return Text(
data.data.toString(),
style: TextStyle(
color: Colors.white70,
),
);
}else{
return Text("No data has been saved");
}
},
),
],
),
)