Devhq provide a lot of services
Connecting a Connect a Dot NET API with Flutter application is a powerful combination for building robust and high-performing cross-platform apps. In this tutorial, we will guide you through the process of setting up CORS in .NET and making API calls from Flutter. Let’s dive in!

Now Lets Know how to Connect a Dot NET API with Flutter
Now are going to know ho to Connect a Dot NET API with Flutter
First Create a Api if you dont know how to create then read this API IN DOTNET
Add a CORS policy to access api from any where
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
C#Now Call the policy
using DesiMart.DbContext;
using DesiMart.Services;
using DesiMart.Services.Interfaces;
using Microsoft.Extensions.Options;
namespace DesiMart
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
// Mongo Db Confugration
builder.Services.Configure<MongoDbConfigs>(builder.Configuration.GetSection("MonogoSetting"));
builder.Services.AddScoped<MongoDbContext>();
// Register ProductService
builder.Services.AddTransient<IProductService, ProductService>();
builder.Services.AddTransient<ICart, CartService>();
builder.Services.AddTransient<IReview, ReviewService>();
builder.Services.AddSwaggerGen();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("AllowAll");
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}
C#Now Call the api in flutter
Future<void> addProduct() async {
try {
var response =
await http.get(Uri.parse("https://localhost:7093/api/Product"));
print(response.body);
} catch (ex) {
print(ex);
}
}
C#