如何解决添加动态Slider值以列出和打印它们并将它们作为帖子发送到服务器
我之前曾经问过这个问题,但这有些不同。
我有动态滑块,可以从列表中获取值,并且可以根据需要正常工作。
我想将它们的值添加到另一个列表中并打印出来。
我编写了此代码,但是不起作用,我不知道我的工作错误在哪里:
Class of new list i want to add values to it :
class MyNewList {
final int id;
final String name;
double degree;
MyNewList(this.id,this.name,this.degree);
}
包含列表和列表值滑块的页面代码:
import 'package:flutter/material.dart';
import './class.dart';
import './class.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),);
}
}
class MyList {
final int id;
final String name;
double degree;
MyList(this.id,this.degree);
}
List _getNewList() {
List newList = [];
MyList newlist1 = MyList(1,'sar',25.0);
newList.add(newlist1);
MyList newlist2 = MyList(2,'Moh',10.0);
newList.add(newlist2);
MyList newlist3 = MyList(3,'John',12.0);
newList.add(newlist3);
return newList;
}
List myNewList = [];
List _newList = _getNewList();
add_Item_to_new_list(newId,newName,value) { // function to add values to new list
MyNewList myNewList = MyNewList(newId,value);
return myNewList;
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
Container(
height: 200,child: ListView.builder(
itemCount: _newList.length,itemBuilder: (ctx,index) {
var newId = _newList[index].id;
var newName = _newList[index].name;
double _value = _newList[index].degree;
return Slider(
min: 0,max: 100,value: _value,onChanged: (double value) {
setState(() {
_value = value;
_newList[index].degree = value;
});
},onChangeEnd: (value) {
add_Item_to_new_list(newId,value); // call function to add values to new list
},);
},),RaisedButton(
child: Text('Insert'),onPressed: () {
myNewList.map((item) {
print(item.degree);
}).toList();
})
],);
}
}
我也想将新的列表数据作为帖子发送到API服务器
解决方法
我通过对此代码进行此更改来解决它:
add_Item_to_new_list(newId,newName,value) { // function to add values to new list
MyNewList myNewList = MyNewList(newId,value);
return myNewList;
}
此代码:
add_Item_to_new_list(newId,value) {
MyNewList newList = MyNewList(newId,value);
myNewList.add(newList);
return myNewList;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。