Unhandled Exception: Could not connect to 127.0.0.1:28015. Error SocketException: Connection refused (OS Error: Connection refused, errno = 111)

410 views Asked by At

i am Trying to connect my flutter chat app with Docker server (Rethinkdb) but the error on top is Showing

Here is my CompositionRoot.dart :

import 'package:chat/chat.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_newapp/src/blocs/onBoarding/onboarding_cubit.dart';
import 'package:flutter_newapp/src/blocs/onBoarding/prfil_image_cubit.dart';
import 'package:flutter_newapp/src/data/Services/image_uploader.dart';
import 'package:flutter_newapp/src/data/Services/local_cache.dart';
import 'package:flutter_newapp/src/presentation/screen/onBoarding/onboarding.dart';
import 'package:rethink_db_ns/rethink_db_ns.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CompositionRoot {
  static late SharedPreferences _sharedPreferences;
  static late RethinkDb _r;
  static late Connection _connection;
  static late IUserService _userService;
  static late ILocalCache _localCache;
  static late ImageUploader imageUploader;

  static configure() async {
    _r = RethinkDb();
    _connection = await _r.connect(host: "127.0.0.1", port: 28015);
    _userService = UserService(_r, _connection);
    _localCache = LocalCache(_sharedPreferences);
  }

  static Widget composeOnboardingUi() {

    ImageUploader imageUploader = ImageUploader('http://localhost:3000/upload');
    OnBoardingCubit onboardingCubit =
        OnBoardingCubit(_userService, _localCache, imageUploader);

    return MultiBlocProvider(providers:[
      BlocProvider(create: (BuildContext context) => onboardingCubit ),
      BlocProvider(create: (BuildContext context) => ProfileImageCubit() )
    ], child: OnBoardingScreen());
  }
}

and here are muy main.dart file :

import 'package:flutter/material.dart';
import 'package:flutter_newapp/core/app_theme.dart';
import 'package:flutter_newapp/src/presentation/screen/onBoarding/onboarding.dart';

import 'composition_root.dart';

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await CompositionRoot.configure();
 runApp(const MyApp());
}

class MyApp extends StatelessWidget {
 const MyApp({super.key});

 // This widget is the root of your application.
 @override
 Widget build(BuildContext context) {
   return MaterialApp(
     title: 'Chat App',
     debugShowCheckedModeBanner: false,
     theme: lightTheme(context),
     darkTheme: darkTheme(context),
     home: CompositionRoot.composeOnboardingUi(),
   );
 }
}

and the rethinkdb is already running as you see :

enter image description here

i Tried to change the 127.0.0.1 by my Machine's ip addresse but the same problem is still showing

1

There are 1 answers

2
Artem Redchych On

"localhost" and "127.0.0.1" should be replaced to "10.0.2.2". It solved this problem for me