英文:
Receive response from DB Laragon
问题
最后的 responseData 转换应更改为:
final List<Map<String, dynamic>> responseData = await conn.query('select * from posts');
你的 _fetch
方法应该如下:
void _fetch() async {
var settings = ConnectionSettings(
host: '10.0.2.2',
port: 3306,
user: 'raf',
password: '123',
db: 'bd',
);
var conn = await MySqlConnection.connect(settings);
var response = await conn.query('select * from posts');
print(response);
final List<Map<String, dynamic>> responseData = await conn.query('select * from posts');
setState(() {
users = responseData.map((e) => User.fromJson(e)).toList();
});
}
这样应该能解决你的问题。
英文:
I recently returned to dart programs with the flutter framework and I am not able to resolve a problem from the response I receive from the laragon database.
I'm having a problem with response.body
This is my code:
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:testedb/userd.dart';
import 'package:mysql1/mysql1.dart';
class UsersPage extends StatefulWidget {
const UsersPage({super.key});
@override
State<UsersPage> createState() => _UsersPageState();
}
class _UsersPageState extends State<UsersPage> {
List<User> users = [];
void _fetch() async {
var settings = ConnectionSettings(
host: '10.0.2.2',
port: 3306,
user: 'raf',
password: '123',
db: 'bd'
);
var conn = await MySqlConnection.connect(settings);
var response = await conn.query('select * from posts');
print(response);
final responseData = jsonDecode(response.body);
setState((){
users = responseData.map((e) => User.fromJson(e)).toList();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
...users.map((user) => ListTile(
leading: Container(
height: 50,
width: 50,
child: CircleAvatar(
backgroundImage: NetworkImage("${user.image}"),
),
),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("${user.name}"),
Text("${user.age}", style: TextStyle(fontSize: 12),),
SizedBox(height: 7,),
],
),
subtitle: Text(user.race, maxLines: 2, overflow: TextOverflow.ellipsis,),
),),
],
),
floatingActionButton: FloatingActionButton(onPressed: _fetch, tooltip: 'Fetch Data', child: Icon(Icons.cloud_download),),
);
}
}
I get the correct answer, but I think I'm not converting correctly to the list (see the error response.body)
thanks for help!
User class
class User {
final int id;
final String name;
final String image;
final String age;
final String race;
User({
required this.id,
required this.name,
required this.image,
required this.age,
required this.race,
});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id : json['id'],
name : json['name'],
image : json['image'],
age : json['age'],
race : json['race'],
);
}
}
答案1
得分: 1
变量 conn 等于等待 MySqlConnection.connect(settings);
变量 results 等于等待 conn.query('select * from posts');
打印(results);
最终行等于 results.toList();
最终用户列表等于行.map((e) => User.fromJson(e.fields)).toList();
设置状态(() {
用户 = 用户列表;
});
英文:
var conn = await MySqlConnection.connect(settings);
var results = await conn.query('select * from posts');
print(results);
final rows = results.toList();
final usersList = rows.map((e) => User.fromJson(e.fields)).toList();
setState(() {
users = usersList;
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论