如何将列表编码为json?
这是我给杰森的课。
class Players{
List<Player> players;
Players({this.players});
factory Players.fromJson(List<dynamic> parsedJson){
List<Player> players = List<Player>();
players = parsedJson.map((i)=>Player.fromJson(i)).toList();
return Players(
players: players,
);
}
}
class Player{
final String name;
final String imagePath;
final int totalGames;
final int points;
Player({this.name,this.imagePath, this.totalGames, this.points});
factory Player.fromJson(Map<String, dynamic> json){
return Player(
name: json['name'],
imagePath: json['imagePath'],
totalGames: json['totalGames'],
points: json['points'],
);
}
}
Run Code Online (Sandbox Code Playgroud)
我设法用fromJson解码,结果在List中。现在,我有另一个播放器要添加json并想将列表编码为json,现在不知道要这样做了。结果总是失败。
var json = jsonDecode(data);
List<Player> players = Players.fromJson(json).players;
Player newPlayer = Player(name: _textEditing.text,imagePath: _imagePath,totalGames: 0,points: 0);
players.add(newPlayer);
String encode = jsonEncode(players.players); …Run Code Online (Sandbox Code Playgroud) 如何更改 PopupMenuButton 上的图标颜色,我已将主题与 iconTheme 一起使用,但它不会影响 CheckedPopupMenuItem 或 PopupMenuItem 上的图标。
Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
elevation: 0.0,
backgroundColor: Colors.transparent,
actions: <Widget>[
Theme(
data: Theme.of(context).copyWith(
cardColor: Colors.indigo,
iconTheme: IconThemeData(color: Colors.white),
),
child: ListTileTheme(
iconColor: Colors.white,
child: PopupMenuButton<String>(
onSelected: _showCheckedMenuSelections,
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
CheckedPopupMenuItem<String>(
value: _checkedValue1,
checked: _showRoles,
child: Text(_checkedValue1, style: Theme.of(context).textTheme.body1),
),
const PopupMenuDivider(),
PopupMenuItem<String>(
value: 'Get Link',
child: ListTile(
leading: Icon(Icons.phonelink),
title: Text('Get link', style: Theme.of(context).textTheme.body1),
),
),
],
),
),
),
],
),
Run Code Online (Sandbox Code Playgroud)
为什么Kotlin中有这样的emptyList构造函数?它是一个不可变的List,所以没有办法添加或删除它的元素,它是空的!那么,这个emptyList的功能是什么?
新来的颤振。我知道如何设置警报对话框的状态,但是由于需要点击 ()=> _createPlayer 之类的功能,它不想重建警报对话框。我想知道如何在需要点击警报对话框时设置状态。
File _image;
GestureDetector(
onTap: () => _createPlayer(),
Run Code Online (Sandbox Code Playgroud)
点击后,它将显示一个警告对话框,如下所示:
_createPlayer() {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0))),
content: Container(
height: 400,
width: 300,
child: Column(
children: <Widget>[
Text('Create Player', style: Theme
.of(context)
.textTheme
.body1),
GestureDetector(
onTap: _getImageCamera,
child: CircleAvatar(
radius: 100,
backgroundColor: Colors.white,
backgroundImage: _image != null ? FileImage(_image) : AssetImage('assets/images/undercover.png'),
),
),
],
),
),
);
});
}
_getImageCamera() async{
var image = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() { …Run Code Online (Sandbox Code Playgroud)