I've been trying for three days to convert the String named description to double but always find some error like invalid double where should i do some changement? can some one help me !
This code defines a Dart class named "Note".
class Note {
int _id;
String _title;
String _description;
String _date;
int _priority, _color;
Note(this._title, this._date, this._priority, this._color,
this._description);
Note.withId(this._id, this._title, this._date, this._priority, this._color,
this._description);
int get id => _id;
String get title => _title;
String get description => _description;
int get priority => _priority;
int get color => _color;
String get date => _date;
set title(String newTitle) {
if (newTitle.length <= 255) {
_title = newTitle;
}
}
set description(String newDescription) {
if (newDescription.length <= 255) {
_description = newDescription;
}
}
set priority(int newPriority) {
if (newPriority >= 1 && newPriority <= 3) {
_priority = newPriority;
}
}
set color(int newColor) {
if (newColor >= 0 && newColor <= 9) {
_color = newColor;
}
}
set date(String newDate) {
_date = newDate;
}
// Convert a Note object into a Map object
Map<String, dynamic> toMap() {
var map = Map<String, dynamic>();
if (id != null) {
map['id'] = _id;
}
map['title'] = _title;
map['description'] = _description;
map['priority'] = _priority;
map['color'] = _color;
map['date'] = _date;
return map;
}
Note.fromMapObject(Map<String, dynamic> map) {
_id = map['id'];
_title = map['title'];
_description = map['description'];
_priority = map['priority'];
_color = map['color'];
_date = map['date'];
}
}
The NoteDetail class is passed an instance of the Note class and the title of the appbar when it is created
note_detail.dart
class NoteDetail extends StatefulWidget {
final String appBarTitle;
final Note note;
const NoteDetail(this.note, this.appBarTitle, {Key key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return NoteDetailState(this.note, this.appBarTitle);
}
}
class NoteDetailState extends State<NoteDetail> {
DatabaseHelper helper = DatabaseHelper();
String appBarTitle;
Note note;
TextEditingController titleController = TextEditingController();
TextEditingController descriptionController = TextEditingController();
int color;
bool isEdited = false;
NoteDetailState(this.note, this.appBarTitle);
@override
Widget build(BuildContext context) {
titleController.text = note.title;
descriptionController.text = note.description;
color = note.color;
return WillPopScope(
onWillPop: () async {
isEdited ? showDiscardDialog(context) : moveToLastScreen();
return false;
},
child: Scaffold(
appBar: AppBar(
elevation: 0,
title: Text(
appBarTitle,
style: Theme.of(context).textTheme.headline5,
),
backgroundColor: colors[color],
leading: IconButton(
splashRadius: 22,
icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
onPressed: () {
isEdited ? showDiscardDialog(context) : moveToLastScreen();
}),
actions: <Widget>[
IconButton(
splashRadius: 22,
icon: const Icon(
Icons.save,
color: Colors.black,
),
onPressed: () {
titleController.text.isEmpty
? showEmptyTitleDialog(context)
: _save();
},
),
IconButton(
splashRadius: 22,
icon: const Icon(Icons.delete, color: Colors.black),
onPressed: () {
showDeleteDialog(context);
},
)
],
),
body: Container(
color: colors[color],
child: Column(
children: <Widget>[
PriorityPicker(
selectedIndex: 3 - note.priority,
onTap: (index) {
isEdited = true;
note.priority = 3 - index;
},
),
ColorPicker(
selectedIndex: note.color,
onTap: (index) {
setState(() {
color = index;
});
isEdited = true;
note.color = index;
},
),
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: titleController,
maxLength: 255,
style: Theme.of(context).textTheme.bodyText2,
onChanged: (value) {
updateTitle();
},
decoration: const InputDecoration.collapsed(
hintText: 'Title',
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
keyboardType: TextInputType.multiline,
maxLines: 10,
maxLength: 255,
controller: descriptionController,
style: Theme.of(context).textTheme.bodyText1,
onChanged: (value) {
updateDescription();
},
decoration: const InputDecoration.collapsed(
hintText: 'Description',
),
),
),
),
],
),
),
));
}
void moveToLastScreen() {
Navigator.pop(context, true);
}
void updateTitle() {
isEdited = true;
note.title = titleController.text;
}
void updateDescription() {
isEdited = true;
note.description = descriptionController.text;
}
// Save data to database
void _save() async {
moveToLastScreen();
note.date = DateFormat.yMMMd().format(DateTime.now());
if (note.id != null) {
await helper.updateNote(note);
} else {
await helper.insertNote(note);
}
}
void _delete() async {
await helper.deleteNote(note.id);
moveToLastScreen();
}
}
note_list.dart
class NoteList extends StatefulWidget {
const NoteList({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return NoteListState();
}
}
class NoteListState extends State<NoteList> {
DatabaseHelper databaseHelper = DatabaseHelper();
List<Note> noteList;
int count = 0;
int axisCount = 2;
@override
Widget build(BuildContext context) {
if (noteList == null) {
noteList = [];
updateListView();
}
Widget myAppBar() {
return AppBar(
title: Text('Notes', style: Theme.of(context).textTheme.headline5),
centerTitle: true,
elevation: 0,
backgroundColor: Colors.white,
leading: noteList.isEmpty
? Container()
: IconButton(
splashRadius: 22,
icon: const Icon(
Icons.search,
color: Colors.black,
),
onPressed: () async {
final Note result = await showSearch(
context: context, delegate: NotesSearch(notes: noteList));
if (result != null) {
navigateToDetail(result, 'Edit Note');
}
},
),
actions: <Widget>[
noteList.isEmpty
? Container()
: IconButton(
splashRadius: 22,
icon: Icon(
axisCount == 2 ? Icons.list : Icons.grid_on,
color: Colors.black,
),
onPressed: () {
setState(() {
axisCount = axisCount == 2 ? 4 : 2;
});
},
)
],
);
}
return Scaffold(
appBar: myAppBar(),
body: noteList.isEmpty
? Container(
color: Colors.white,
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text('Click on the add button to add a new note!',
style: Theme.of(context).textTheme.bodyText2),
),
),
)
: Container(
color: Colors.white,
child: getNotesList(),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
navigateToDetail(Note('', '', 3, 0,''), 'Add Note');
},
tooltip: 'Add Note',
shape: const CircleBorder(
side: BorderSide(color: Colors.black, width: 2.0)),
child: const Icon(Icons.add, color: Colors.black),
backgroundColor: Colors.white,
),
);
}
Widget getNotesList() {
return StaggeredGridView.countBuilder(
physics: const BouncingScrollPhysics(),
crossAxisCount: 4,
itemCount: count,
itemBuilder: (BuildContext context, int index) => GestureDetector(
onTap: () {
navigateToDetail(noteList[index], 'Edit Note');
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: colors[noteList[index].color],
border: Border.all(width: 2, color: Colors.black),
borderRadius: BorderRadius.circular(8.0)),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
noteList[index].title,
style: Theme.of(context).textTheme.bodyText2,
),
),
),
Text(
getPriorityText(noteList[index].priority),
style: TextStyle(
color: getPriorityColor(noteList[index].priority)),
),
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: Text(
noteList[index].description ?? '',
style: Theme.of(context).textTheme.bodyText1),
)
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(noteList[index].date,
style: Theme.of(context).textTheme.subtitle2),
])
],
),
),
),
),
staggeredTileBuilder: (int index) => StaggeredTile.fit(axisCount),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
);
}
// Returns the priority color
Color getPriorityColor(int priority) {
switch (priority) {
case 1:
return Colors.red;
break;
case 2:
return Colors.yellow;
break;
case 3:
return Colors.green;
break;
default:
return Colors.yellow;
}
}
// Returns the priority icon
String getPriorityText(int priority) {
switch (priority) {
case 1:
return '!!!';
break;
case 2:
return '!!';
break;
case 3:
return '!';
break;
default:
return '!';
}
}
void navigateToDetail(Note note, String title) async {
bool result = await Navigator.push(context,
MaterialPageRoute(builder: (context) => NoteDetail(note, title)));
if (result == true) {
updateListView();
}
}
void updateListView() {
final Future<Database> dbFuture = databaseHelper.initializeDatabase();
dbFuture.then((database) {
Future<List<Note>> noteListFuture = databaseHelper.getNoteList();
noteListFuture.then((noteList) {
setState(() {
this.noteList = noteList;
count = noteList.length;
});
});
});
}
}
i solve it by change this instruction
descriptionController.text = note.description;
to
note.description = double.tryParse(descriptionController.text)?? 00.00;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.