Saya harus mendapatkan data dari server MySQL menjadi bergetar dan meneruskannya ke widget. tapi itu tidak berfungsi dalam kode saya. Setelah saya memeriksa kesalahan dan saya menemukan beberapa URL berfungsi contoh (//https://api.github.com/users) URL ini berfungsi. tapi saya tidak tahu apa alasannya tidak berfungsi di URL saya dalam kode flutter

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart'as http;

class SudentClassView extends StatefulWidget {
@override
_SudentClassViewState createState() => _SudentClassViewState();
}

class _SudentClassViewState extends State<SudentClassView> {

Future<List> getData() async{


var url = Uri.parse('http://apitwo.studysmile.lk/apicrudphp/api/read.php');
final response = await http.get(url);
return json.decode(response.body);
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(title: Text('Home Menu'),),
  body:FutureBuilder<List>(
      future: getData(),
      builder: (ctx,ss){
        if(ss.hasError)
        {
          print('error');
        }
        if(ss.hasData)
        {
          return Classes(list:ss.data);
        }
        else
        {
          return CircularProgressIndicator();
          /* Center(
            child: CircularProgressIndicator(),
          );*/
        }
      }
  ),
);
}
}

 class Classes extends StatelessWidget {
 List list;
 Classes({this.list});

 @override
 Widget build(BuildContext context) {
 return ListView.builder(
    itemCount: list == null?0:list.length,
    itemBuilder: (ctx,i){print(list.length);
      return Container(
        alignment: Alignment.center,
        margin: EdgeInsets.all(20),
        // ignore: deprecated_member_use
        child: FlatButton(
          child: Align(
              alignment: Alignment(0.2, 0.6),
              child: Text('${list[i]['name']}', style: TextStyle(fontSize: 20.0),)
          ),
          color: Color(0xff7c4dff),
          textColor: Colors.white,
          onPressed: (){print(list[i]['name']);
            //   Navigator.of(context).push(
            //     MaterialPageRoute(builder: (BuildContext context)=>StudentSubject(list:list,index:i))

            //);print("Student Class ID " + list[i]['fldStudentClassID']);
          },


        ),
      );
    }
);
}
}

Ini kode php saya, saya pikir ini sudah berfungsi

<?php
header("Content-type: application/json; charset=utf-8");
require_once('db.php');
$query = 'SELECT * FROM `employee`';
$stm = $db->prepare($query);
$stm->execute();
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
0
kavinda 27 Mei 2021, 07:43

1 menjawab

Jawaban Terbaik

MENCOBA

https://apitwo.studysmile.lk/apicrudphp/api/read.php

1
Rohit Parmar 27 Mei 2021, 04:52