StreamBuilderで定期的に使用する場合;
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
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 _streamController = StreamController();
Timer _timer;
Future getData() async {
var url = 'https://milk-white-reveille.000webhostapp.com/get.php';
http.Response response = await http.get(url);
var data = jsonDecode(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(
stream: _streamController.stream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData)
return ListView(
children: snapshot.data.map((document) {
return ListTile(
title: Text(document['title']),
subtitle: Text(document['type']),
);
}).toList(),
);
return Text('Loading...');
},
),
);
}
}