Coder Social home page Coder Social logo

utillibs / mini_server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cristianpaulo4/mini_server

1.0 0.0 0.0 129 KB

License: MIT License

Kotlin 0.34% Swift 1.10% Objective-C 0.10% Dart 17.84% HTML 10.47% CMake 22.38% C++ 45.77% C 2.00%

mini_server's Introduction

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

Step - 2


Import the dependencies

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

Step - 3


Create an instance

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

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);
  });

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,
              ),
            ),
          );
        },
      ),
    );
  }
}


mini_server's People

Contributors

cristianpaulo4 avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.