我在Geting data from mysql to StreamBuilder Flutter的Flutter应用程序遇到了问题
在这里how to force the server to respond as json
现在一切都很好,但是StreamBuilder
仅传递了表中的第一行,我该怎么办。
最新的抖动代码:-
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Server',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: MyHomePage(title: 'Flutter Server App'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
StreamController<Map> _streamController = StreamController<Map>();
Timer _timer;
Future getData() async {
var url = 'https://milk-white-reveille.000webhostapp.com/get.php';
http.Response response = await http.get(url);
Map data = json.decode(response.body);
//Add your data to stream
_streamController.add(data);
}
@override
void initState() {
super.initState();
getData();
//Check the server every 5 seconds
_timer = Timer.periodic(Duration(seconds: 5), (timer) => getData());
}
@override
void dispose() {
//cancel the timer
if (_timer.isActive) _timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
centerTitle: true,
),
body: StreamBuilder(
stream: _streamController.stream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) return Text('Loading...');
return ListView(
children: [
ListTile(
title: Text(snapshot.data['title']),
subtitle: Text(snapshot.data['type']),
),
],
);
},
),
);
}
}
最新的PHP代码:-
get.php
<?php
header("Content-type: application/json; charset=utf-8");
require_once('db.php');
$query = 'SELECT * FROM `flutter` WHERE 1';
$stm = $db->prepare($query);
$stm->execute();
$row = $stm->fetch(PDO::FETCH_ASSOC);
echo json_encode($row);
$row = $stm->fetch(PDO::FETCH_ASSOC);
echo json_encode($row);
db.php
<?php
$dns = 'mysql:host=localhost;dbname=id13424627_flutter';
$user = 'id13424627_flutter_app';
$pass = 'Flutter_maen12';
try{
$db = new PDO ($dns, $user, $pass);
}catch( PDOException $e){
$error = $e->getMessage();
echo $error;
}
答案 0 :(得分:2)
您应该在服务器端进行的更改;
<?php
header("Content-type: application/json; charset=utf-8");
require_once('db.php');
$query = 'SELECT * FROM `flutter`';
$stm = $db->prepare($query);
$stm->execute();
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
您的颤动代码;
class _MyHomePageState extends State<MyHomePage> {
StreamController<List> _streamController = StreamController<List>();
Timer _timer;
Future getData() async {
var url = 'https://milk-white-reveille.000webhostapp.com/get.php';
http.Response response = await http.get(url);
List data = json.decode(response.body);
//Add your data to stream
_streamController.add(data);
}
@override
void initState() {
getData();
//Check the server every 5 seconds
_timer = Timer.periodic(Duration(seconds: 5), (timer) => getData());
super.initState();
}
@override
void dispose() {
//cancel the timer
if (_timer.isActive) _timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("widget.title"),
centerTitle: true,
),
body: StreamBuilder<List>(
stream: _streamController.stream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData)
return ListView(
children: [
for (Map document in snapshot.data)
ListTile(
title: Text(document['title']),
subtitle: Text(document['type']),
),
],
);
return Text('Loading...');
},
),
);
}
}
答案 1 :(得分:0)
检查$query = 'SELECT * FROM
颤动 WHERE 1';
WHERE 1
删除以显示或更改为 WHERE id='whatever you want to display only'