颤音:type' list< string,object>>>'不是类型的子类型' list< string,int>>>'在类型中

发布于 2025-02-08 13:00:31 字数 4202 浏览 0 评论 0原文

在使用fluttervs code作为IDE构建测验应用程序的AA原型期间,我收到以下错误:

键入'list'list< map< string,object&gt ;>'不是类型的“列表”< map< string,int>>' 在类型中cast

在我的quiz.dart文件中,

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

import './answer.dart';
import './question.dart';

class Quiz extends StatelessWidget {
  final Function? fun;
  final int? questionIndex;
  final List<Map<String, Object>>? array;

  Quiz({required this.fun, required this.questionIndex, required this.array});
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Question(
          (array)![questionIndex!]['questionText'] as String,
        ),
        ...((array)![questionIndex!]['answers'] as List<Map<String, int>>)
            .map((question) {
          return Answer((() => (fun!(question['score'])) as VoidCallback),
              (question['text'] as String));
        }).toList(),
        // ...[Text('hello'), Text('okay')],
      ],
    );
  }
}

,以下是我的main.dart文件,其中一个人可以找到我的插件array >在测验 widget类的构造函数中。

import 'package:flutter/material.dart';

import './quiz.dart';
import './result.dart';
// void main() {
//   runApp(MyApp());
// }

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  final _ques = const [
    {
      'questionText': 'What\'s your fav. color?',
      'answers': [
        {'text': 'Black', 'score': 10},
        {'text': 'Red', 'score': 5},
        {'text': 'Green', 'score': 3},
        {'text': 'White', 'score': 1},
      ]
    },
    {
      'questionText': 'What\'s your fav. animal?',
      'answers': [
        {'text': 'Cat', 'score': 5},
        {'text': 'Dog', 'score': 3},
        {'text': 'Snake', 'score': 4},
        {'text': 'Bird', 'score': 2}
      ]
    },
    {
      'questionText': 'Who\'s your fav. instructor?',
      'answers': [
        {'text': 'Max', 'score': 1},
        {'text': 'Max', 'score': 1},
        {'text': 'Max', 'score': 1}
      ]
    },
  ];

  var _questionIndex = 0;
  var _totalScore = 0;

  void _hola(int score) {
    _totalScore += score;

    setState(() => _questionIndex = _questionIndex + 1);
    print(_questionIndex);
    if (_questionIndex < _ques.length) {
      print('We have more questions!!');
    }
  }

  @override
  Widget build(BuildContext context) {
    // print('hpllll ${context}');

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Ayoo App Bar stuff'),
          backgroundColor: Colors.black,
        ),
        body: _questionIndex < _ques.length
            ? Quiz(questionIndex: _questionIndex, array: _ques, fun: _hola)
            : Result(_totalScore),
      ),
    );
  }
}

实际上,即使我用来

final List<Map<String,List<Map<String,int>>>> _ques = const [
    {
      'questionText': 'What\'s your fav. color?',
      'answers': [
        {'text': 'Black', 'score': 10},
        {'text': 'Red', 'score': 5},
        {'text': 'Green', 'score': 3},
        {'text': 'White', 'score': 1},
      ]
    },
    {
      'questionText': 'What\'s your fav. animal?',
      'answers': [
        {'text': 'Cat', 'score': 5},
        {'text': 'Dog', 'score': 3},
        {'text': 'Snake', 'score': 4},
        {'text': 'Bird', 'score': 2}
      ]
    },
    {
      'questionText': 'Who\'s your fav. instructor?',
      'answers': [
        {'text': 'Max', 'score': 1},
        {'text': 'Max', 'score': 1},
        {'text': 'Max', 'score': 1}
      ]
    },
  ];

明确定义datatype _ques list,我也会收到错误。以下是一个屏幕截图显示错误

”在此处输入图像描述”

在修复/解释此错误方面的任何帮助将受到高度赞赏!

During construction of a a prototype for a quiz app using Flutter and VS Code as the IDE, I got the following error:

type 'List<Map<String, Object>>' is not a subtype of type 'List<Map<String, int>>' in type cast

from my quiz.dart file which is

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

import './answer.dart';
import './question.dart';

class Quiz extends StatelessWidget {
  final Function? fun;
  final int? questionIndex;
  final List<Map<String, Object>>? array;

  Quiz({required this.fun, required this.questionIndex, required this.array});
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Question(
          (array)![questionIndex!]['questionText'] as String,
        ),
        ...((array)![questionIndex!]['answers'] as List<Map<String, int>>)
            .map((question) {
          return Answer((() => (fun!(question['score'])) as VoidCallback),
              (question['text'] as String));
        }).toList(),
        // ...[Text('hello'), Text('okay')],
      ],
    );
  }
}

Following is my main.dart file where one can find my plugin for array in the constructor of Quiz widget class.

import 'package:flutter/material.dart';

import './quiz.dart';
import './result.dart';
// void main() {
//   runApp(MyApp());
// }

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  final _ques = const [
    {
      'questionText': 'What\'s your fav. color?',
      'answers': [
        {'text': 'Black', 'score': 10},
        {'text': 'Red', 'score': 5},
        {'text': 'Green', 'score': 3},
        {'text': 'White', 'score': 1},
      ]
    },
    {
      'questionText': 'What\'s your fav. animal?',
      'answers': [
        {'text': 'Cat', 'score': 5},
        {'text': 'Dog', 'score': 3},
        {'text': 'Snake', 'score': 4},
        {'text': 'Bird', 'score': 2}
      ]
    },
    {
      'questionText': 'Who\'s your fav. instructor?',
      'answers': [
        {'text': 'Max', 'score': 1},
        {'text': 'Max', 'score': 1},
        {'text': 'Max', 'score': 1}
      ]
    },
  ];

  var _questionIndex = 0;
  var _totalScore = 0;

  void _hola(int score) {
    _totalScore += score;

    setState(() => _questionIndex = _questionIndex + 1);
    print(_questionIndex);
    if (_questionIndex < _ques.length) {
      print('We have more questions!!');
    }
  }

  @override
  Widget build(BuildContext context) {
    // print('hpllll ${context}');

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Ayoo App Bar stuff'),
          backgroundColor: Colors.black,
        ),
        body: _questionIndex < _ques.length
            ? Quiz(questionIndex: _questionIndex, array: _ques, fun: _hola)
            : Result(_totalScore),
      ),
    );
  }
}

In fact, I get an error even when I use

final List<Map<String,List<Map<String,int>>>> _ques = const [
    {
      'questionText': 'What\'s your fav. color?',
      'answers': [
        {'text': 'Black', 'score': 10},
        {'text': 'Red', 'score': 5},
        {'text': 'Green', 'score': 3},
        {'text': 'White', 'score': 1},
      ]
    },
    {
      'questionText': 'What\'s your fav. animal?',
      'answers': [
        {'text': 'Cat', 'score': 5},
        {'text': 'Dog', 'score': 3},
        {'text': 'Snake', 'score': 4},
        {'text': 'Bird', 'score': 2}
      ]
    },
    {
      'questionText': 'Who\'s your fav. instructor?',
      'answers': [
        {'text': 'Max', 'score': 1},
        {'text': 'Max', 'score': 1},
        {'text': 'Max', 'score': 1}
      ]
    },
  ];

to explicitly define the datatype for _ques list. Following is a screenshot showing the error

enter image description here

Any help in regard to fixing/explaining this error is highly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆沫 2025-02-15 13:00:31

由于类型:

List<Map<String,List<Map<String,int>>>>

您创建了一个地图列表,其中左侧始终是字符串,右侧必须始终是映射列表&lt; string,int&gt;。
我建议改用以下类型:

List<Map<String, dynamic>>

Because of the type :

List<Map<String,List<Map<String,int>>>>

You created a list of map where the left Side are always String and the Right side must be always List of Map<String, int>.
I suggest to use the following type instead :

List<Map<String, dynamic>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文