未处理的异常:类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型 'String' 的子类型 (cognitive_face_flutter)

问题描述

我使用了示例中给出的确切代码片段。它仍然抛出错误未处理的异常:类型 '_InternalLinkedHashMap' is not a subtype of type 'String' over the line

List<Face> _faces = await client.detect(
        image: _image,returnFaceAttributes: FaceAttributeType.values,returnFaceLandmarks: true,);

有人能告诉我如何纠正这个问题并从 API 获取 faceAttribute 值,无论 client.detect 返回什么。

完整代码如下:

final endpoint = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0";
final key = "3080b54bd7c64a27801465608ca06a3e";

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',theme: ThemeData(
        primarySwatch: Colors.blue,),home: MyHomePage(title: 'Flutter Demo Home Page'),);
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key,this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File image;
  List<Face> faces = [];
  bool isLoading = false;
  final client = FaceServiceClient(key,serviceHost: endpoint);

  void _incrementCounter() async {
    setState(() {
      image = null;
      faces = [];
    });
    var maxWidth = MediaQuery.of(context).size.width;
    File _image = await ImagePicker.pickImage(
      source: ImageSource.gallery,maxWidth: maxWidth,);

    if (_image != null) {
      _getimageSize(Image.file(_image,fit: BoxFit.fitWidth)).then((Size size) {
        print('CROPPED IMAGE WIDTH: ${size.width} HEIGHT: ${size.height}');
      });

      setState(() {
        image = _image;
        isLoading = true;
      });

      List<Face> _faces = await client.detect(
        image: _image,);
      print('DETECTED FACES: ${_faces.length}');

      setState(() {
        faces = _faces;
        isLoading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),body: Column(
        children: <Widget>[
          _buildImage(context),FlatButton(
            child: Text('Clear'),onpressed: () {
              setState(() {
                image = null;
                faces = null;
              });
            },)
        ],floatingActionButton: FloatingActionButton(
        onpressed: _incrementCounter,tooltip: 'Increment',child: Icon(Icons.add),// This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  Widget _buildImage(BuildContext context) {
    if (image != null) {
      return Container(
        // height: MediaQuery.of(context).size.width,width: MediaQuery.of(context).size.width,child: FutureBuilder<Size>(
          future: _getimageSize(Image.file(image,fit: BoxFit.fitWidth)),builder: (BuildContext context,AsyncSnapshot<Size> snapshot) {
            if (snapshot.hasData) {
              return Stack(children: [
                Container(
                  width: snapshot.data.width,height: snapshot.data.height,foregrounddecoration: FaceDetectdecoration(
                    faces,snapshot.data,child: Image.file(
                    image,fit: BoxFit.fitWidth,isLoading
                    ? Center(
                        child: CircularProgressIndicator(),)
                    : Container(),]);
            } else {
              return Text('Please wait...');
            }
          },);
    }

    return Container(
      height: MediaQuery.of(context).size.height - 250.0,alignment: Alignment.center,child: Text('no picture'),);
  }

  Future<Size> _getimageSize(Image image) {
    Completer<Size> completer = new Completer<Size>();
    image.image.resolve(new ImageConfiguration()).addListener(
        (ImageInfo info,bool _) => completer.complete(
            Size(info.image.width.todouble(),info.image.height.todouble())));
    return completer.future;
  }
}

解决方法

问题似乎是由代码中 FaceAttributeType.values 的类型不匹配引起的。

您将代码更改为:

Map data= new Map<String,dynamic>.from(FaceAttributeType.values);

List<Face> _faces = await client.detect(
    image: _image,returnFaceAttributes: data,returnFaceLandmarks: true,);

然后再试一次。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...