0

I have to get data from MySQL server into a flutter and pass it to widgets. but it's not working in my code. After I'm checking errors and I found some URLs are working example (//https://api.github.com/users) this URL working . but I don't know what the is the reason not working in my URL in flutter code

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']);
          },


        ),
      );
    }
);
}
}

This is my php code i think it's already working

<?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);
2
  • what is error you are getting? Commented May 27, 2021 at 10:54
  • this is a my error "I/flutter (21941): error" Commented May 27, 2021 at 12:40

1 Answer 1

1

TRY

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm double check and I got it. your correct. its was a cloudflare issue in my server. thank you very much . you save my live

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.