i have this kind of prob, i want integrate Flutter with .Net core web API. im new to flutter so, i just followed the tutorial but the i cant connected to localhost api
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:testproject_flutter/Service/model/models.dart';
class ServiceDataProvider {
final _baseUrl = 'http://localhost:5000/api';
final http.Client httpClient;
ServiceDataProvider({required this.httpClient});
Future<Service> createService(Service service) async {
final response = await httpClient.post(
Uri.http('http://localhost:5000', '/api/services'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, dynamic>{
"serviceName": service.ServiceName,
"description": service.Description,
"category": service.Category,
"initialPrice": service.InitialPrice,
"intermediatePrice": service.IntermediatePrice,
"advancedPrice": service.AdvancedPrice,
}),
);
if (response.statusCode == 200) {
return Service.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to create course.');
}
}
Future<List<Service>> getServices() async {
try {
final response = await http.get('$_baseUrl/services' as Uri);
if (response.statusCode == 200) {
final services = jsonDecode(response.body) as List;
return services.map((service) => Service.fromJson(service)).toList();
} else {
throw Exception('Failed to load courses');
}
} catch (e) {
throw Exception("Exception throuwn $e");
}
}
Future<void> deleteService(int id) async {
final http.Response response = await http.delete(
'http://localhost:5000/Service/$id' as Uri,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode != 200) {
throw Exception('Failed to delete course.');
}
}
Future<void> updateService(Service service) async {
final http.Response response = await httpClient.put(
'http://localhost:5000/Service/' as Uri,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, dynamic>{
"serviceId": service.id,
"serviceName": service.ServiceName,
"description": service.Description,
"category": service.Category,
"initialPrice": service.InitialPrice,
"intermediatePrice": service.IntermediatePrice,
"advancedPrice": service.AdvancedPrice,
}),
);
if (response.statusCode != 200) {
throw Exception('Failed to update course.');
}
}
}
this is service_data.dart from flutter i copy and modified to follow latest flutter package, i try to connect to several address to replace localhost
with 0.0.0.0
, 192.168.42.232
, 127.0.0.1
but still has no changed, this is my program.cs , i use .net7 controller api
using fixit.Data;
using fixit.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddDbContext<DataContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("WebApiDatabase")));
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddCors(option =>
{
option.AddPolicy("allowedOrigin",
builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()
);
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddScoped<IRepository<Service>, ServiceRepository>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
/*var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://localhost:5000", "http://192.168.0.243:5000")
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); */
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder => builder
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
);
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();
i have my xampp on i try android emulation and real device, but localhost still cant connect, i also have minimal api one to connect to flutter but still the same, how to solve this?