我不确定如何最好地解释此问题,但我会尽力而为。我面临的问题是,每当我单击模拟器上的按钮转到下一页时,我都会遇到很多错误,但是应用程序本身可以正常运行而没有任何问题,仅当单击该按钮时才会出现错误终点站。错误显示如下:
E/dalvikvm( 1787): Could not find class 'android.view.inputmethod.CursorAnchorInfo$Builder', referenced from method io.flutter.plugin.editing.InputConnectionAdaptor.finishComposingText
W/dalvikvm( 1787): VFY: unable to resolve new-instance 411 (Landroid/view/inputmethod/CursorAnchorInfo$Builder;) in Lio/flutter/plugin/editing/InputConnectionAdaptor;
D/dalvikvm( 1787): VFY: replacing opcode 0x22 at 0x000e
D/dalvikvm( 1787): DexOpt: unable to opt direct call 0x0846 at 0x10 in Lio/flutter/plugin/editing/InputConnectionAdaptor;.finishComposingText
在主页上单击按钮时,会弹出这些错误。代码如下:
主页的按钮代码:
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => SharedPreference1()));
},
该按钮会转到另一个屏幕,在该屏幕上处理TextField的输入,该屏幕工作正常,但错误是在Home
类中预先单击该按钮时发生的。
整个代码如下:
import 'package:shared_preferences/shared_preferences.dart';
TextEditingController _notesController1 = new TextEditingController();
TextEditingController _notesController2 = new TextEditingController();
List<String> data = [];
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
));
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return (Scaffold(
drawer: Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: Text(
'Glass',
style: TextStyle(
fontSize: 15.0,
letterSpacing: 1.0,
color: Colors.black,
fontFamily: 'Montserrat',
),
),
),
ListTile(
title: Text('Trash'),
),
],
),
),
backgroundColor: Colors.blueGrey[700],
appBar: AppBar(
title: Text(
'Glass',
style: TextStyle(
fontSize: 20.0,
letterSpacing: 1.0,
color: Colors.white,
fontFamily: 'Montserrat',
),
),
centerTitle: true,
backgroundColor: Colors.blueGrey[700],
),
floatingActionButton: FloatingActionButton(
elevation: 9.0,
child: Icon(Icons.add),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => SharedPreference1()));
},
backgroundColor: Colors.blueGrey[300],
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
));
}
}
Future<bool> saveData(String nameKey, String value) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return await preferences.setString(nameKey, value);
}
Future<String> loadData(String nameKey) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return preferences.getString(nameKey);
}
class Hero extends State<SharedPreference1> {
Widget buildSaveButton(context) {
return Container(
color: Colors.blueGrey[700],
margin: EdgeInsets.only(top:340.0),
child: RaisedButton.icon(
elevation: 9.0,
icon: Icon(Icons.save),
label: Text('Save'),
color: Colors.white,
onPressed: () async {
await saveData("_key_name", _notesController2.text);
await setData();
print(data);
},
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.blueGrey[700],
child: SafeArea(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
buildHeading(context),
buildNotesText(),
buildSaveButton(context),
],
),
),
),
),
);
}
@override
void initState() {
super.initState();
setData();
}
setData() {
loadData("_key_name").then((value) {
setState(() {
if(value==null){
print("Value not available.");
}
else{
data.add(value);
}
});
});
}
}
Widget buildHeading(context) {
return Material(
color: Colors.blueGrey[700],
child: Padding(
padding: const EdgeInsets.only(left: 20.0, top: 10.0),
child: Row(
children: <Widget>[
Expanded(
child: TextField(
maxLines: 1,
controller: _notesController1,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Note Title',
),
style: TextStyle(fontSize: 20, color: Colors.white, fontFamily: 'Montserrat',),
),
),
FlatButton(
child: Icon(Icons.close, color: Colors.white, size: 27),
onPressed: () {
Navigator.of(context).pop();
},
)
],
),
),
);
}
Widget buildNotesText() {
return Material(
color: Colors.blueGrey[700],
child: Padding(
padding: const EdgeInsets.all(20.0),
child: TextField(
maxLines: null,
controller: _notesController2,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Create Note Here',
),
cursorColor: Colors.white,
autofocus: true,
style: TextStyle(color: Colors.white,fontSize: 18,fontFamily: 'Montserrat'),
),
),
);
}
class SharedPreference1 extends StatefulWidget {
SharedPreference1() : super();
@override
Hero createState() => Hero();
}```
答案 0 :(得分:0)
我不确定是什么原因引起的错误,但是您代码中的某些内容应该更改。您应该始终在类内部包含方法和变量,否则它们将处于全局范围内,并且永远不要丢弃。
每次创建控制器时,都需要对其进行正确创建和处理(通常在initState
和dispose
方法中)。
我对您的代码进行了一些重构,以在您的类中包括所有全局范围的方法/变量。看看这是否可以解决您的问题。
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
),
);
}
class Hero extends State<SharedPreference1> {
TextEditingController _notesController1;
TextEditingController _notesController2;
List<String> data = [];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.blueGrey[700],
child: SafeArea(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
buildHeading(context),
buildNotesText(),
buildSaveButton(context),
],
),
),
),
),
);
}
Widget buildHeading(context) {
return Material(
color: Colors.blueGrey[700],
child: Padding(
padding: const EdgeInsets.only(left: 20.0, top: 10.0),
child: Row(
children: <Widget>[
Expanded(
child: TextField(
maxLines: 1,
controller: _notesController1,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Note Title',
),
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontFamily: 'Montserrat',
),
),
),
FlatButton(
child: Icon(Icons.close, color: Colors.white, size: 27),
onPressed: () {
Navigator.of(context).pop();
},
)
],
),
),
);
}
Widget buildNotesText() {
return Material(
color: Colors.blueGrey[700],
child: Padding(
padding: const EdgeInsets.all(20.0),
child: TextField(
maxLines: null,
controller: _notesController2,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Create Note Here',
),
cursorColor: Colors.white,
autofocus: true,
style: TextStyle(
color: Colors.white, fontSize: 18, fontFamily: 'Montserrat'),
),
),
);
}
Widget buildSaveButton(context) {
return Container(
color: Colors.blueGrey[700],
margin: EdgeInsets.only(top: 340.0),
child: RaisedButton.icon(
elevation: 9.0,
icon: Icon(Icons.save),
label: Text('Save'),
color: Colors.white,
onPressed: () async {
await saveData("_key_name", _notesController2.text);
await setData();
print(data);
},
),
);
}
setData() {
loadData("_key_name").then((value) {
setState(() {
if (value == null) {
print("Value not available.");
} else {
data.add(value);
}
});
});
}
Future<bool> saveData(String nameKey, String value) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return await preferences.setString(nameKey, value);
}
Future<String> loadData(String nameKey) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return preferences.getString(nameKey);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return (Scaffold(
drawer: Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: Text(
'Glass',
style: TextStyle(
fontSize: 15.0,
letterSpacing: 1.0,
color: Colors.black,
fontFamily: 'Montserrat',
),
),
),
ListTile(
title: Text('Trash'),
),
],
),
),
backgroundColor: Colors.blueGrey[700],
appBar: AppBar(
title: Text(
'Glass',
style: TextStyle(
fontSize: 20.0,
letterSpacing: 1.0,
color: Colors.white,
fontFamily: 'Montserrat',
),
),
centerTitle: true,
backgroundColor: Colors.blueGrey[700],
),
floatingActionButton: FloatingActionButton(
elevation: 9.0,
child: Icon(Icons.add),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => SharedPreference1()));
},
backgroundColor: Colors.blueGrey[300],
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
));
}
}
class SharedPreference1 extends StatefulWidget {
SharedPreference1() : super();
@override
Hero createState() => Hero();
}