android - 面对 Flutter 中的按钮 onclick 问题
问题描述
我不能 100% 确定如何最好地解释这个问题,但我会尽力而为。我面临的问题是,每当我单击模拟器上的按钮转到下一页时,我都会遇到很多错误,但是应用程序本身可以正常运行而没有任何问题,只有单击按钮时才会出现错误终点站。错误如下所示:
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();
}```
解决方案
我不确定是什么导致了错误,但您的代码中有一些内容应该更改。您应该始终在类中包含方法和变量,否则它们在全局范围内并且永远不会被释放。
每当您创建控制器时,都需要正确创建和处理它(通常在您的initState
anddispose
方法中)。
我对您的代码进行了一些重构,以在您的类中包含所有全局范围的方法/变量。看看这是否能解决您的问题。
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();
}
推荐阅读
- ssl - 使用 Wireshark 解密来自应用程序的 SSL/TLS 流量
- tensorflow - Tensorflow OD API - 微调模型时使用 dropout
- javascript - 如何不让'$'影响使用 python 或 JS 在 MongoDB 中插入文档?
- docker - 新模型是否有与 Tensorflow-Serving 中的新版本相同的“加载策略”?
- c# - 禁用 DropDownList 并能够将值传递给更新模型
- c++ - 从对向量中按值擦除元素
- android - Android Studio:如何在布局 xml 文件中自动添加结束/结束标记
- python - 将 Tenor 的索引和值组合成一个新的张量
- javascript - 父位置更改时JQuery删除元素
- ios - 如何使用 React-Native 在 iOS 中禁用屏幕截图