类型列表动态不是类型映射字符串动态的子类型



这是显示类型列表动态不是类型映射字符串动态的子类型"现在,当我运行模拟器时;类型列表dynamic不是类型映射字符串dynamic"的子类型;在我的模拟器上,我如何修复它?

现在我想创建一个关于从我的模拟器中查找api id的程序,当我输入一些数字时,使用文本字段和按钮,点击按钮将显示该id的数据或标题,但我只是初学者,所以如果有人认为我的代码是正确的或不正确的,或者你有一些建议,请告诉我
我的JSON

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<Album> fetchAlbum() async {
final response =
await http.get(Uri.parse('http://192.168.176.131:3000/api/courses/'));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return Album.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception1
throw Exception('Failed to load album');
}
}
class Album {
final int userId;
final int id;
final String title;
Album({
required this.userId,
required this.id,
required this.title,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);

@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late Future<Album> futureAlbum;
@override
void initState() {
super.initState();
futureAlbum = fetchAlbum();
}
Widget build(BuildContext context) {
TextEditingController searchController = new TextEditingController();
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Fetch Data Example'),
),
body: Column(children: [
Container(
child: Row(children: [
Expanded(
flex: 2,
child: TextField(
controller: searchController
)
),
Expanded(
flex: 1,
child: FlatButton(
onPressed: () {
print(
"this is the text to search for => ${searchController
.text}");
},
child: Text("Search"),
)
)
],)
),
Center(
child: FutureBuilder<Album>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!.title);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
),
],)
),
);
}
}

JSON代码

const Joi = require('joi');
const express = require('express');
const app = express();
app.use(express.json());
const courses = [
{ userId:1, id:1, title:'course 1'},
{ userId:2, id:2, title:'course 2'},
{ userId:3, id:3, title:'course 3'},
]; 
app.get('/', (req,res)  => {
res.send('Hello world');
});
app.get('/api/courses',(req, res) => {
res.send(courses);
});
app.post('/api/courses', (req, res) => {   
const { error }  = validateCourse(req.body);
if (error)return res.status(400).send(error.details[0].message);

const course = {
id: courses.length +1,
name: req.body.name
};
courses.push(course);
res.send(course);
});
app.put('/api/courses/:id', (req, res) => {
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) return res.status(404).send('The course with the given ID was not found.');
const {error} = validateCourse(req.body); //result.eror
if (error) return res.status(400).send(error.details[0].message);


//Update course
course.name = req.body.name;
res.send(course);
//Return the updated course
});

app.delete('/api/courses/:id', (req, res) => {
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) return res.status(404).send('The course with the given ID was not found.');
const index = courses.indexOf(course);
courses.splice(index, 1);
res.send(course);
});
function validateCourse(course){
const schema = {
name: Joi.string().min(3).required()
};
return Joi.validate(course, schema);
}

app.get('/api/curses/:id',(req,res(=>{const course=courses.find(c=>c.id===parseInt(req.params.id((;if(!course(return res.status(404(.send('找不到具有给定ID的课程。'(;res.send(课程(;});

const port=process.env.port||3000;//app.learn(端口,((=>console.log(Listening on port ${port} ..((;app.learn(3000,0.0.0.0(;

您应该将jsonDecode的返回值强制转换为Map <String, dynamic>。jsonDecode返回一个dynamic值,而不是Album.fromJson所期望的Map <String, dynamic>

相关内容

  • 没有找到相关文章

最新更新