Java – How to connect the SpringBoot backend to flutter

flutterjavaspring-boot

I'm working on an app that uses a spring-boot backend for the application methods and I'm using flutter to develop my frontend. I made the frontend pages that I required but I am unsure how to make HTTP calls from flutter to my backend

I tried looking at youtube videos and other stack overflow problems that were similar to what I'm doing but I still can't figure it out.

For example, this is the backend code for the login authentication

/**
     * Controller method that attempts to login
     * 
     * @param username
     * @param password
     * @return ResponseEntity
     */
    @GetMapping("/auth/{username}/{password}")
    public ResponseEntity login(@PathVariable("username") String username, @PathVariable("password") String password)
            throws Exception {
        // No exception thrown means the authentication succeeded
        try {
            authentication.login(username, password);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Response(false, e.getMessage()));
        }
        return ResponseEntity.status(HttpStatus.OK).body(new Response(true, "Login successful"));
    }

and this is the what I included in Flutter

...
String url = 'http://localhost:8080/users/auth/{username}/{password}';

  Future<String> login() async {
    var response = await http.get(Uri.encodeFull(url), headers:{"Accept" : "application/json"});
    Navigator.of(context).pushNamed('/signup');
  }
...

...

child: MaterialButton(
        minWidth: MediaQuery.of(context).size.width,
        padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
        onPressed: () {
          login();
        },
        child: Text("Login",
        textAlign: TextAlign.center,
        style: style.copyWith(
          color: Colors.white, fontWeight: FontWeight.bold)),
      ),

...

This is the error that appears

E/flutter ( 3945): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Invalid argument(s): No host specified in URI /auth/%7Busername%7D/%7Bpassword%7D
E/flutter ( 3945): #0      _HttpClient._openUrl (dart:_http/http_impl.dart:2117:9)
E/flutter ( 3945): #1      _HttpClient.openUrl (dart:_http/http_impl.dart:2043:7)
E/flutter ( 3945): #2      IOClient.send (package:http/src/io_client.dart:33:36)
E/flutter ( 3945): <asynchronous suspension>
E/flutter ( 3945): #3      BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
E/flutter ( 3945): <asynchronous suspension>
E/flutter ( 3945): #4      BaseClient.get (package:http/src/base_client.dart:32:7)
E/flutter ( 3945): #5      get.<anonymous closure> (package:http/http.dart:46:36)
E/flutter ( 3945): #6      _withClient (package:http/http.dart:166:20)
E/flutter ( 3945): <asynchronous suspension>
E/flutter ( 3945): #7      get (package:http/http.dart:46:5)
E/flutter ( 3945): #8      _MyHomePageState.login (package:calcount_mobile/main.dart:65:26)
E/flutter ( 3945): <asynchronous suspension>
E/flutter ( 3945): #9      _MyHomePageState.build.<anonymous closure> (package:calcount_mobile/main.dart:102:11)
E/flutter ( 3945): #10     _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:511:14)
E/flutter ( 3945): #11     _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:566:30)
E/flutter ( 3945): #12     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24)
E/flutter ( 3945): #13     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:240:9)
E/flutter ( 3945): #14     TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:177:9)
E/flutter ( 3945): #15     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:436:9)
E/flutter ( 3945): #16     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter ( 3945): #17     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter ( 3945): #18     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:221:19)
E/flutter ( 3945): #19     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:199:22)
E/flutter ( 3945): #20     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 3945): #21     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 3945): #22     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 3945): #23     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 3945): #24     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 3945): #25     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 3945): #26     _invoke1 (dart:ui/hooks.dart:233:10)
E/flutter ( 3945): #27     _dispatchPointerDataPacket (dart:ui/hooks.dart:154:5)
E/flutter ( 3945):

Best Solution

  1. If you are testing your spring boot backend with flutter Emulator device then url should be String url = 'http://10.0.2.2:8080/users/auth/{username}/{password}';

  2. If you are testing your backend with real android phone then, define inbound firewall rule (in windows defender firewall ) and allow the port 80 in firewall rule. get the ip address of your system, and change the url to String url = 'http://ip:8080/users/auth/{username}/{password}';

Related Question