接收来自 Laragon 数据库的响应

huangapple go评论61阅读模式
英文:

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 &#39;dart:convert&#39;;
import &#39;dart:io&#39;;
import &#39;package:http/http.dart&#39; as http;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:testedb/userd.dart&#39;;
import &#39;package:mysql1/mysql1.dart&#39;;


class UsersPage extends StatefulWidget {
  const UsersPage({super.key});

  @override
  State&lt;UsersPage&gt; createState() =&gt; _UsersPageState();
}

class _UsersPageState extends State&lt;UsersPage&gt; {
  List&lt;User&gt; users = [];
  void _fetch() async {
    
   var settings = ConnectionSettings(
  host: &#39;10.0.2.2&#39;, 
  port: 3306,
  user: &#39;raf&#39;,
  password: &#39;123&#39;,
  db: &#39;bd&#39;
);
var conn = await MySqlConnection.connect(settings);
var response = await conn.query(&#39;select * from posts&#39;);
print(response);
final responseData = jsonDecode(response.body);


setState((){
    users = responseData.map((e) =&gt; User.fromJson(e)).toList();
});
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          ...users.map((user) =&gt; ListTile(
            leading: Container(
              height: 50,
              width: 50,
              child: CircleAvatar(
                backgroundImage: NetworkImage(&quot;${user.image}&quot;),
              ),
            ),
            title: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(&quot;${user.name}&quot;),
                Text(&quot;${user.age}&quot;, style: TextStyle(fontSize: 12),),
                SizedBox(height: 7,),
              ],
            ),
            subtitle: Text(user.race, maxLines: 2, overflow: TextOverflow.ellipsis,),
          ),),
        ],
      ),
      floatingActionButton: FloatingActionButton(onPressed: _fetch, tooltip: &#39;Fetch Data&#39;, 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&lt;String, dynamic&gt; json) {
    return User(
        id : json[&#39;id&#39;],
        name : json[&#39;name&#39;],
        image : json[&#39;image&#39;],
        age : json[&#39;age&#39;],
        race : json[&#39;race&#39;], 
    );
  }
}

接收来自 Laragon 数据库的响应

答案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(&#39;select * from posts&#39;);
  print(results);
  final rows = results.toList();
  final usersList = rows.map((e) =&gt; User.fromJson(e.fields)).toList();
  setState(() {
    users = usersList;
  });

huangapple
  • 本文由 发表于 2023年5月7日 20:43:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定