The argument type 'List' can't be assigned to the parameter type 'Widget? Function(BuildContext, int)'.
Missing selector such as '.identifier' or '[0]'.
The argument type 'CupertinoListTile' can't be assigned to the parameter type 'dynamicFunction(dynamic)'.
Undefined name 'e'.
Undefined class 's'.
Expected to find ';'.
I'm getting above errors. how to solve this
select_county.dart
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") )]),
)
],
)
);
}
}
countrycodes.json
[{
"name": "Afganistan",
"dial_code" : " 93"
},
{
"name": "Albaina ",
"dial_code" : " 355"
},
{
"name": "Algeria ",
"dial_code" : " 358"
}
]
CodePudding user response:
instead of SliverChildBuilderDelegate you have to use SliverChildListDelegate because you are passing the list not the builder function. and replace map(.. with map<Widget>(.. so the compiler knows the map only returns widgets.
so the final code looks like this:
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"))]),
)
],
));
}
}
CodePudding user response:
Try this :
@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")),
],
),
);
}

