ylliX - Online Advertising Network
How to build GenAI mock server?

I can’t read data from ASP.net web API into my Flutter App


I am trying to perform basic read operation in ASP.net web API.why can’t I get my data from the API which i deployed on ngrok .The data is stored in MicrosoftSQL Server Studio.My flutter code is in Flutlab which is in an online IDE.
The API is still running but I can’t read the data from API.

apihandler.dart


 

import 'dart:convert';
import 'package:piapp/model.dart';
import 'package:http/http.dart' as http;

class ApiHandler {
  final String baseUri =
      "https://4861-2409-4073-4d86-68df-6cd4-2174-c52e-1aee.ngrok-free.app/api/Users";

  Future<List<Users>> getUserData() async {
    List<Users> data = [];

    final uri = Uri.parse(baseUri);
    try {
      final response = await http.get(
        uri,
        headers: <String, String>{
          'Content-type': 'application/json; charset=UTF-8',
        },
      );

      if (response.statusCode >= 200 && response.statusCode <= 299) {
        final List<dynamic> jsonData = json.decode(response.body);
        data = jsonData.map((json) => Users.fromJson(json)).toList();
      }
    } catch (e) {
      return data;
    }
    return data;
  }

}

this is the file which performs flutter UI

import 'package:flutter/material.dart';
import 'apihandler.dart';

import 'model.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  ApiHandler apiHandler = ApiHandler();
  late List<Users> data = [];

  void getData() async {
    data = await apiHandler.getUserData();
    setState(() {});
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          ListView.builder(
            shrinkWrap: true,
            itemCount: data.length,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                onTap: () {
                  ();
                },
                leading: Text("${data[index].id}"),
                title: Text(data[index].name),
                subtitle: Text(data[index].password),
              );
            },
          ),
        ],
      ),
    );
  }
}





Can anyone tell me what’s wrong in my code ?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *