У меня была проблема с моим приложением флаттера здесь Получение данных из mysql в StreamBuilder Flutter и здесь как заставить сервер отвечать как 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;
}