抖动错误:参数类型'Future <img/>'无法分配给参数类型'Widget'

时间:2019-02-03 13:31:30

标签: dart flutter

我正在使用Flutter Flame Library并收到此错误

The argument type 'Future<Image>' can't be assigned to the parameter
type 'Widget'.

我的代码是:

import 'package:flutter/material.dart';
import 'package:flame/flame.dart';

class TileMap extends StatefulWidget {
  @override
  _TileMapState createState() => _TileMapState();
}

class _TileMapState extends State<TileMap> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Flame.images.load('grass_05.png'),
    );
  }
}

如何解决? 有什么问题吗? 谢谢大家

1 个答案:

答案 0 :(得分:1)

class _TileMapState extends State<TileMap> {
  @override
  void initState() {
    super.initState();
    _loadImage()
  }

  void _loadImage() await {
    var img = await Flame.images.load('grass_05.png');
    setState(() => _image = img);
  }

  Image image;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _image,
    );
  }
}