Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Mendapatkan data dari mysql ke StreamBuilder Flutter

Jika Anda ingin menggunakannya secara berkala dengan 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...');
        },
      ),
    );
  }
}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Pemecahan masalah Tidak ada file atau direktori saat menjalankan `php app/console doktrin:skema:buat`

  2. Bagaimana saya bisa menghitung jumlah baris yang dikembalikan oleh kueri MySQL?

  3. MySQL:Apa yang dimaksud dengan % di kolom host dan bagaimana mengubah kata sandi pengguna

  4. PHP mysqli menyiapkan pernyataan untuk prosedur tersimpan tanpa parameter

  5. TAMPILKAN TABEL pernyataan dengan beberapa nilai LIKE