mini_server 1.1.0 copy "mini_server: ^1.1.0" to clipboard
mini_server: ^1.1.0 copied to clipboard

Mini http server.

This package was developed to create a server while your flutter application is running. Ex: imagine the following scenario, a desktop app with Sqlite database was developed and the need arises to create a mobile app to consume this same data, as long as it is on the same local network, this package is able to carry out this communication.

Note: Not tested in large applications. Note: This server only works while the application is open.

Features #

  • GET
  • POST
  • PUT
  • DELETE

Step -1 #

add dependecy in pubspec
copied to clipboard

Step - 2 #


Import the dependencies

import 'dart:io';
import 'package:mini_server/mini_server.dart';

copied to clipboard

Step - 3 #


Create an instance

final miniServer = MiniServer(
    host: 'localhost',
    port: 8080,
  );

copied to clipboard

Step - 4 #


Generate your routes

  miniServer.get("/", (HttpRequest httpRequest) async {
    value.value++;
    return httpRequest.response.write(value.value);
  });

  miniServer.post("/test", (HttpRequest httpRequest) async {
    MiniResponse res = await MiniResponse.instance.init(httpRequest);
    return httpRequest.response.write(res.parameters);
  });

  miniServer.post("/test02", (HttpRequest httpRequest) async {
    final res = await MiniResponse.instance.init(httpRequest);
    return httpRequest.response.write(res.body);
  });

copied to clipboard

Example #


Note : If you want to test the example, replace the "localhost" with the current "IP" of the computer. Access your mobile browser on the url add your IP with the previously configured port, ex: 10.0.0.145:8080. keep updating the page and see the counter increment.

import 'dart:io';
import 'package:mini_server/mini_server.dart';
import 'package:flutter/material.dart';

ValueNotifier<int> value = ValueNotifier(0);
void main() {
  final miniServer = MiniServer(
    host: 'localhost',
    port: 8080,
  );

  miniServer.get("/", (HttpRequest httpRequest) async {
    value.value++;
    return httpRequest.response.write(value.value);
  });

  miniServer.post("/test", (HttpRequest httpRequest) async {
    MiniResponse res = await MiniResponse.instance.init(httpRequest); 
    return httpRequest.response.write(res.parameters);
  });

  miniServer.post("/test02", (HttpRequest httpRequest) async {
    final res = await MiniResponse.instance.init(httpRequest);
    return httpRequest.response.write(res.body);
  });

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ValueListenableBuilder(
        valueListenable: value,
        builder: (_, value, __) {
          return Center(
            child: Text(
              "$value",
              style: const TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 100,
              ),
            ),
          );
        },
      ),
    );
  }
}


copied to clipboard
20
likes
130
points
93
downloads

Publisher

unverified uploader

Weekly Downloads

2024.10.02 - 2025.04.16

Mini http server.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on mini_server