Flutter List.map()时获取索引值index

list =

List songListByUser = [
  {“score”: “A-“, “歌名”: “童年”, ‘id’: 1},
  {“score”: “C+”, “歌名”: “之乎者也”, ‘id’: 2},
  {“score”: “B+”, “歌名”: “野百合春天”, ‘id’: 3},
  {“score”: “D+”, “歌名”: “恋曲1990”, ‘id’: 4},
  {“score”: “D+”, “歌名”: “恋曲”, ‘id’: 5},
  {“score”: “B-“, “歌名”: “歌”, ‘id’: 6},
];
    return list
        .asMap()
        .keys
        .map((value) => ListTile(
              leading: Icon(
                Icons.music_note,
                color: Colors.green,
              ),
              trailing: Icon(Icons.keyboard_arrow_right),
              title: Text(list[value][‘歌名’].toString()),
              onTap: () {
                var mapstr = json.encode(list[value]).toString();
              },
            ))
        .toList();

Flutter 动态更新CircleAvatar的图片

CircleAvatar有一个参数是 backgroundImage, 它是一个Imageprovider. 通过选择手机图片或者拍照后,获取到的是一个PickedFile , 我用的是

image_picker: ^0.6.7+17插件
PickedFile _imageFile;
    ImagePicker imagePicker = ImagePicker();
    var image = await imagePicker.getImage(source: imageSource);
    setState(() {
      _imageFile = image;
      _avatarImage = File(image.path);
    });
定义一个文件类型的变量_avatarImage, 然后通过Image.file(_avatarImage).image 赋值给backgroundImage就行了。AssetImage(“assets/images/cat.jpg”)是默认值。
File _avatarImage;
backgroundImage: _avatarImage == null
                      ? AssetImage(“assets/images/cat.jpg”)
                      : Image.file(_avatarImage).image,

Flutter 与Android 的通讯 -BasicMessageChannel方式

Flutter 端

static const BasicMessageChannel<String> messageChannel = BasicMessageChannel<String>(“flutter-drumkaka”, StringCodec());
var receivedMessage = “”
//messageChannel.setMessageHandler(_handlePlatformIncrement);
Future<String> _handleNativeMessage(String message) async {
print(“message=$message”);
setState(() {

receivedMessage = message;
});
return “a return message”;
}
_handNativeMessage(“this is from Flutter”);

 

Android 端

String CHANNEL = "flutter-drumkaka";
BasicMessageChannel<String> messageChannel = new BasicMessageChannel<>(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL, StringCodec.INSTANCE);
messageChannel.
        setMessageHandler(new BasicMessageChannel.MessageHandler<String>() {

            @Override
            public void onMessage(@Nullable String message, @NonNull BasicMessageChannel.Reply<String> reply) {
                showMessage(message);                
            }
        });

messageChannel.send("this is from Android Native");