当前位置: 首页 > 知识库问答 >
问题:

参数类型“List”无法分配给参数类型“Widget”?-颤振

农雅畅
2023-03-14

参数类型“List”不能分配给参数类型“Widget?函数(BuildContext, int)”。

缺少选择器,如“”。标识符“”或“[0]”。

参数类型“库比蒂诺列表”不能分配给参数类型“动态函数(动态)”。

未定义的名称“e”。

未定义的类's'。

应找到“;”。

我正在克服错误。如何解决这个问题

选择\u county。飞奔

import 'dart:convert';

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

class Selectcountry extends StatefulWidget {
  const Selectcountry({Key? key}) : super(key: key);

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

class _SelectcountryState extends State<Selectcountry> {

List<dynamic>?dataRetrieved;
List<dynamic>? data;

  @override
  void initState(){
    _getData();
  }
  
  Future _getData()async{
    final String response = await rootBundle.loadString('assets/CountryCodes.json');
    dataRetrieved= await json.decode(response)as List<dynamic>;
    setState(() {
      data =dataRetrieved;
    });
  }
  

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(child:CustomScrollView(
      slivers: [
        CupertinoSliverNavigationBar(largeTitle: Text("Select Country"),
        previousPageTitle: "Edit Number",),
        SliverList(
          delegate: SliverChildBuilderDelegate((data != null)
              ? data!
              .map((e) = CupertinoListTile(
            onTap: (){
              print(e['name']);
            },
            title: Text(e['name']),
            trailing: Text(e['dial_code']),
          )).toList()
               :[Center (child: Text("Loading") )]),
              )
      ],
    )
    );
  }
}

国家代码。json

[{

  "name": "Afganistan",
  "dial_code" : "+93"
},
  {
    "name": "Albaina ",
    "dial_code" : "+355"
  },
  {
    "name": "Algeria ",
    "dial_code" : "+358"
  }
]

共有2个答案

蔚桐
2023-03-14

尝试以下操作:

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: CustomScrollView(
        slivers: [
          CupertinoSliverNavigationBar(
            largeTitle: Text("Select Country"),
            previousPageTitle: "Edit Number",
          ),
          data != null
              ? SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (context, index) {
                      var item = data?[index];
                      return CupertinoListTile(
                        onTap: () {
                          print(item['name']);
                        },
                        title: Text(item['name']),
                        trailing: Text(item['dial_code']),
                      );
                    },
                    childCount: data!.length,
                  ),
                )
              : Center(child: Text("Loading")),
        ],
      ),
    );
  }
楚畅
2023-03-14

您必须使用SliverChildListDelegate,而不是SliverChildBuilderDelegate,因为您传递的是列表,而不是生成器函数。并将地图(..替换为地图

import 'dart:convert';

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

class Selectcountry extends StatefulWidget {
  const Selectcountry({Key? key}) : super(key: key);

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

class _SelectcountryState extends State<Selectcountry> {
  List<dynamic>? dataRetrieved;
  List<dynamic>? data;

  @override
  void initState() {
    _getData();
  }

  Future _getData() async {
    final String response =
        await rootBundle.loadString('assets/CountryCodes.json');
    dataRetrieved = await json.decode(response) as List<dynamic>;
    setState(() {
      data = dataRetrieved;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
        child: CustomScrollView(
      slivers: [
        CupertinoSliverNavigationBar(
          largeTitle: Text("Select Country"),
          previousPageTitle: "Edit Number",
        ),
        SliverList(
          delegate: SliverChildListDelegate(data != null
              ? data!
                  .map<Widget>((e) => CupertinoListTile(
                        onTap: () {
                          print(e['name']);
                        },
                        title: Text(e['name']),
                        trailing: Text(e['dial_code']),
                      ))
                  .toList()
              : [Center(child: Text("Loading"))]),
        )
      ],
    ));
  }
}

 类似资料: