![](/img/trans.png)
[英]Facing an issue where duplication of values occurs through TextField after button onclick
[英]Facing an issue with button onclick in Flutter
我不能 100% 確定如何最好地解釋這個問題,但我會盡力而為。 我面臨的問題是,每當我單擊模擬器上的按鈕到 go 到下一頁時,我都會遇到很多錯誤,但應用程序本身可以正常運行而沒有任何問題,只有當單擊按鈕時才會出現錯誤終點站。 錯誤如下所示:
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
class 中預先單擊該按鈕時。
整個代碼如下:
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();
}```
我不確定是什么導致了錯誤,但您的代碼中有一些內容應該更改。 您應該始終在類中包含方法和變量,否則它們在全局 scope 中並且永遠不會被釋放。
每當您創建 controller 時,都需要正確創建和處置它(通常在您的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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.