我无法在我的 API 应用程序中显示。我无法在localhost上运行是啥原因?

Posted

技术标签:

【中文标题】我无法在我的 API 应用程序中显示。我无法在localhost上运行是啥原因?【英文标题】:I couldn't show in my API application. I couldn't run on localhost What is the reason?我无法在我的 API 应用程序中显示。我无法在localhost上运行是什么原因? 【发布时间】:2021-07-21 15:24:01 【问题描述】:

我用 .NET 编写了 API。但是我没有在 Localhost 中调用我的 Flutter。如何使用自己的 IP 地址。如何从 api 列出我的数据?如何在 Postman 中列出您自己的绳索上的数据? 我对应用程序中的代码有问题。我把相关代码分享给你。

模型.dart

import 'dart:convert';

List<Sonuc> sonucFromJson(String str) => List<Sonuc>.from(json.decode(str).map((x) => Sonuc.fromJson(x)));

String sonucToJson(List<Sonuc> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Sonuc 
  Sonuc(
    this.employeeId,
    this.employeeName,
    this.department,
    this.mailId,
    this.column1,
  );

  int employeeId;
  String employeeName;
  String department;
  String mailId;
  DateTime column1;

  factory Sonuc.fromJson(Map<String, dynamic> json) => Sonuc(
    employeeId: json["employeeID"],
    employeeName: json["employeeName"],
    department: json["department"],
    mailId: json["mailID"],
    column1: DateTime.parse(json["Column1"]),
  );

  Map<String, dynamic> toJson() => 
    "employeeID": employeeId,
    "employeeName": employeeName,
    "department": department,
    "mailID": mailId,
    "Column1": "$column1.year.toString().padLeft(4, '0')-$column1.month.toString().padLeft(2, '0')-$column1.day.toString().padLeft(2, '0')",
  ;

Service.dart

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:rehber_uygulmasi_staj/modeller/sonuc.dart';


class Sonuclar 
  Future<List<Sonuc>> getSonuc() async 
    final String _url = "https://localhost:44333/api/employee";

    final Map<String, String> _headers = 'Accept': 'application/json', 'Content-Type': 'application/json', 'Charset': 'utf-8';

    final http.Response response = await http.get(
      Uri.encodeFull(_url),
      headers: _headers,
    );

    if (response.statusCode == 200) 
      final _jsonBody = response.body;
      print("department: " + _jsonBody);
      final sonucResponse = sonucFromJson(_jsonBody);

      return sonucResponse;
     else 
      throw Exception("Request failed with status: $response.statusCode");
    
  

view.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:rehber_uygulmasi_staj/modeller/sonuc.dart';
import 'package:rehber_uygulmasi_staj/servisler/sonuc.dart';

class AramaSonuc extends StatefulWidget 
    @override
  _AramaSonucState createState() => _AramaSonucState();

class _AramaSonucState extends State<AramaSonuc> 
  final sonucService= Sonuclar();
  Future<List<Sonuc>> _future;
@override
  void initState() 
  _future = sonucService.getSonuc();
  super.initState();
  
  @override
  Widget build(BuildContext context) 
    return Scaffold(
        appBar: AppBar(backgroundColor:Colors.redAccent /*HexColor("#0e2d85")*/, title:  Text("Arama Sonucu",style: TextStyle(fontSize: 24),),),
      body: Container(
        child: FutureBuilder<List<Sonuc>>(
          future: _future,
            builder: (context, AsyncSnapshot<List<Sonuc>> snapshot)
            if(snapshot.hasData)
              return ListView.builder(
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemCount: snapshot.data.length,
                itemBuilder: (context,int index)
                  print("Data is here $snapshot.data");
                  return Card(
                    elevation: 4,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: ListTile(
                      leading: Text(snapshot.data[index].department),
                      title: Text(snapshot.data[index].employeeName),
                      isThreeLine: true,
                      subtitle: Text('$snapshot.data[index].mailId\n$snapshot.data[index].employeeId'),
                    ),
                  );
                
              );
            
            else 
              print("Data is here $snapshot.data");
              return Center(
                child: CircularProgressIndicator(),
              );
            
          
        ),
      ),
    );
  

【问题讨论】:

【参考方案1】:

您是否尝试过此处描述的解决方案?

How to point to localhost:8000 with the Dart http package in Flutter?

var client = createHttpClient();
client.get(new Uri.http("locahost:44333", "/api"));

【讨论】:

不,我不知道如何应用它。 非常感谢,我解决了 ngrok 的问题..

以上是关于我无法在我的 API 应用程序中显示。我无法在localhost上运行是啥原因?的主要内容,如果未能解决你的问题,请参考以下文章

无法在我的反应 js 应用程序中执行 PUT 请求

无法在我的网格中加载 api 值

即使我获得了 API 密钥,也无法使用谷歌地图

我无法在 vuejs 中使用 v-for 从 API 中显示数据

无法在我的地理位置 api 结果中获得 *MAP*

无法在我的颤振演示应用程序中的列表视图中显示网格视图