我们如何从 MySQL 数据库中获取图像并在 Flutter 中显示? 我尝试了很多事情,但我无法做到。我转换了 blob 图像,但这也不起作用。
而且我也无法显示整个数据,即使它在列表视图中......! 下面是它的代码 -:
import 'package:education/Screens/Home_View.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
import 'dart:typed_data';
class ScheduleClass extends StatefulWidget {
@override
_ScheduleClassState createState() => _ScheduleClassState();
}
class _ScheduleClassState extends State<ScheduleClass> {
var course_id, class_id, dates, schedule_image;
var data;
Future<http.Request> getData() async
{
var client = new http.Client();
final response = await client.get('http://192.168.0.104:5000/class_schedule');
final responseJson = json.decode(response.body);
setState(() {
dynamic courseid = responseJson['courseid'];
dynamic classid = responseJson['classid'];
dynamic date = responseJson['date'];
dynamic scheduleimage = responseJson['scheduleimage'];
var blob = responseJson['scheduleimage'];
Uint8List image = Base64Codec().decode(blob); // image is a Uint8List
class_id = classid;
course_id = courseid;
dates = date;
schedule_image = image;
data = responseJson;
});
print(responseJson);
client.close();
// return responseJson;
}
int i = 1;
@override
void initState(){
getData();
super.initState();
print([class_id].length);
}
@override
Widget build(BuildContext context) {
print({class_id}.length.toString());
return Scaffold(
appBar: AppBar(
title: Text('Class Schedules'),
backgroundColor: Colors.deepPurpleAccent,
centerTitle: true,
elevation: 15,
leading: InkWell(
child: Icon(Icons.arrow_back_ios),
onTap: (){
Navigator.pushReplacement(context, MaterialPageRoute(
builder: (context) => homeView()
));
},
),
),
body: ListView.separated(
itemCount: [class_id].length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: Card(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: Text(
'${dates[i]}',
style: TextStyle(
fontSize: 20,
),
),
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
height: 500,
child: Center(
child: Image.memory(
schedule_image[i], //'img/ico.jpg',
fit: BoxFit.fill,
),
),
),
),
],
),
),
);
},
separatorBuilder: (context, index) {
return Divider();
},
),
);
}
}
下面是我要显示的数据。
I/flutter ( 4830): {alternateno: [7845659877, 9865358577], coachaddress: [anand vihar, delhi, delhi, Gautam Budha nagar, noida], coachemail: [abc@gmail.com, xyz@gmail.com], coachid: [1, 2], coachname: [abc Coaching, xyz Coaching], primary_number: [9865328899, 9898557898], userid: [1, 1]}
请帮忙...