reown-com/reown_flutter

IMagicService is already registered

Closed this issue · 5 comments

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Type IMagicService is already registered inside GetIt.
E/flutter ( 5090): #0 throwIfNot (package:get_it/get_it_impl.dart:14:19)
E/flutter ( 5090): #1 _GetItImplementation._register (package:get_it/get_it_impl.dart:1571:11)
E/flutter ( 5090): #2 _GetItImplementation.registerSingleton (package:get_it/get_it_impl.dart:930:5)
E/flutter ( 5090): #3 new ReownAppKitModal (package:reown_appkit/modal/appkit_modal_impl.dart:197:13)
E/flutter ( 5090): #4 _MyHomePageState._initializeAppKitModal (package:trybe/home/homepage.dart:30:20)
E/flutter ( 5090): #5 _MyHomePageState.build. (package:trybe/home/homepage.dart:96:25)
E/flutter ( 5090): #6 _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1183:21)
E/flutter ( 5090): #7 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:315:24)
E/flutte\

Can you share your new code?

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:reown_appkit/reown_appkit.dart';
import 'package:trybe/album/albumpage.dart';
import 'package:trybe/models/imagemodel.dart';
import 'package:trybe/showImage/view.dart';

class MyHomePage extends StatefulWidget {
   const MyHomePage({super.key,});
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  
  late ReownAppKitModal _appKitModal;
  bool _isAppKitModalInitialized = false; 
  
  @override
  void initState() {
    super.initState();
    
    _initializeAppKitModal();  
  }

  Future<void> _initializeAppKitModal() async {
    _appKitModal = ReownAppKitModal(
      context: context,
      projectId: '632b21c33e29272fac6f8b94eca76878',
      metadata: const PairingMetadata(
        name: 'Example App',
        description: 'Example app description',
        url: 'https://reown.com/',
        icons: ['https://reown.com/logo.png'],
        redirect: Redirect(
          native: 'com.example.trybe',
          universal: 'https://reown.com/exampleapp',
        ),
      ),
    );

    // Initialize the modal and set state when done
    await _appKitModal.init();
    setState(() {
      _isAppKitModalInitialized = true;
    });
  }
  @override
  Widget build(BuildContext context) {
   
 Size size = MediaQuery.of(context).size;
   return Scaffold(
      backgroundColor:  Colors.white,
    appBar: PreferredSize(
        preferredSize: const Size.fromHeight(50),
        child: AppBar(
          surfaceTintColor: Colors.white,
        
          leading: Padding(
            padding: const EdgeInsets.only(left: 10),
            child: Image.asset('assets/png/pfp.png'),
          ),
          actions: [
            GestureDetector(
              onTap: (){
               
              },
              child: Padding(
                padding: const EdgeInsets.only(right:kIsWeb ? 30 : 20),
                child: GestureDetector(
                  onTap:()async{
               await _appKitModal.init();
                  },
                  child: Container(
                    height: size.height/18,
                    width: kIsWeb ? size.width/8 : size.width/3.5,
                    decoration: const BoxDecoration(
                      color: kIsWeb ? Colors.green : Colors.transparent,
                    borderRadius: BorderRadius.all(Radius.circular(8))
                    ),
                    child:  Row(
                      // mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        
         AppKitModalConnectButton(
          appKit:_appKitModal,
         custom: InkWell(
                 onTap: ()async{
                if (_appKitModal.status.isInitialized & _isAppKitModalInitialized) {
                  debugPrint('connected');
             _appKitModal.openModalView();
                } else {
                  await _initializeAppKitModal(); // Fallback: initialize if not done yet
                }

  },
  child: const Text(
    'Connect Wallet',
    textAlign: TextAlign.center,
    style: TextStyle(
      color: Colors.black,
      fontSize: 15,
      fontWeight: FontWeight.w700,
    ),
  ),
),

          ),
            
            Visibility(
              visible: _appKitModal.isConnected,
              child: AppKitModalAccountButton(
                appKit: _appKitModal,
                context: context,
              ),
            ),
                      ],
                    ),
                  ),
                ),
              ),
            )
          ],
          flexibleSpace: const  Row(
            children: [
              Padding(
                padding: EdgeInsets.only(left: 50,right: 8,top: kIsWeb ? 8 : 64,bottom:8),
                child: Text(
                  kIsWeb ?'Trybe': '',
                  textAlign: TextAlign.start,
                  style: TextStyle(
                    color: Colors.black,
                    fontWeight: FontWeight.w600,
                    fontSize: 26,
                  ),
                ),
              ),
            ],
          ),
       
          foregroundColor: const Color.fromARGB(255, 255, 251, 251),
          backgroundColor: const Color.fromARGB(255, 255, 255, 251),
          shadowColor: const Color.fromARGB(255, 255, 255, 255),
        ),
      ),
      body: Column(
        children: [
           
          // _buildAlbumDescription(context),
          Expanded(
            child: MasonryGridView.builder(
              itemCount: images.length,
              padding: const EdgeInsets.only(top: 20),
               gridDelegate: const  SliverSimpleGridDelegateWithFixedCrossAxisCount(
                crossAxisCount:kIsWeb ? 4 : 1),
              itemBuilder: (context, index){
               return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: GestureDetector(
                    onTap: (){
                      Get.dialog(
                        ShowImage(index)
                      );

                    },
                    child: Padding(
                      padding: const EdgeInsets.only(bottom: 20,left: 15,right: 15),
                      child: Container(
                        decoration: const  BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(8)),
                          boxShadow: [
                            BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.08),
                            blurRadius: 15.5,
                            spreadRadius: 4
                            )
                          ]
                        ),
                        child: Column(
                          children: [
                          Container(
                            height: 50,
                            decoration: const BoxDecoration(
                              color: Color.fromRGBO(243, 242, 242, 1),
                              borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20))
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                SizedBox(
                                  child: Padding(
                                    padding: const EdgeInsets.only(left: 24),
                                    child: Row(
                                                     children: [
                                       const CircleAvatar(),
                                       const SizedBox(width: 10,),
                                              Text(
                                              'Oxsangz.eth',
                                            style: GoogleFonts.montserratAlternates(
                                            textStyle:const TextStyle(
                                    fontSize: 15,
                                    fontWeight: FontWeight.w500,
                                    color: Color.fromRGBO(0, 0, 0, 1)
                                                                  )
                                                                ),
                                                                )
                                                                   ],
                                    ),
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.only(right: 24),
                                  child: SizedBox(
                                    child: SvgPicture.asset('assets/svg/globeicon.svg'),
                                  ),
                                )
                              
                            ],),
                          ),
                             Image.asset(images[index].image,fit: BoxFit.cover,),
                             Container(
                              height: 230,
                              decoration: const  BoxDecoration(
                                color: Color.fromRGBO(255, 255, 255, 1),
                                borderRadius: BorderRadius.only(bottomLeft: Radius.circular(20),bottomRight: Radius.circular(20))
                              ),
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.spaceAround,
                                children: [
                                  SvgPicture.asset('assets/svg/indicator.svg'),
                                  Padding(
                                    padding: const EdgeInsets.only(left: 18,right:18),
                                    child: Text('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Quis ipsum suspendisse ultrices gravida. Risus commodo viverra maecenas accumsan lacus vel facilisi',
                                    style: GoogleFonts.montserratAlternates(
                                      textStyle:const TextStyle(
                                        fontSize: 12.25,
                                        fontWeight: FontWeight.w500,
                                        color: Color.fromRGBO(0, 0, 0, 1)
                                      )
                                    ),
                                    ),
                                  ),
                                  Row(
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.only(left: 18,),
                                        child: Text('1hr ago',
                                        style: GoogleFonts.montserratAlternates(
                                          textStyle: const TextStyle(
                                            fontWeight: FontWeight.w500,
                                            fontSize: 11.25,
                                            color: Color.fromRGBO(0, 0, 0, 1)
                                          )
                                        ),),
                                      ),
                                    ],
                                  ),

                                  Padding(
                                    padding: const EdgeInsets.only(bottom: 20),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        Padding(
                                          padding: const EdgeInsets.only(left: 24),
                                          child: SizedBox(
                                            child: Row(
                                            
                                              children: [
                                                SvgPicture.asset('assets/svg/likeicon.svg'),
                                                Padding(
                                                  padding: const EdgeInsets.only(left: 8,right: 8),
                                                  child: SvgPicture.asset('assets/svg/loveicon.svg'),
                                                ),
                                                SvgPicture.asset('assets/svg/bookmakicon.svg'),
                                                              
                                              ],
                                            ),
                                          ),
                                        ),
                                        Padding(
                                          padding: const EdgeInsets.only(right: 24),
                                          child: SizedBox(
                                            child: Row(
                                              children: [
                                                SizedBox(
                                                     child: Column(
                                                         children: [
                                                          Row(
                                                            children: [
                                                              Text('0.002',
                                                              style:GoogleFonts.montserratAlternates(
                                                                textStyle:const TextStyle(
                                                                  fontWeight: FontWeight.w500,
                                                                  fontSize: 12,
                                                                  color:  Color.fromRGBO(0, 0, 0, 1)
                                                                )
                                                              ) ,),
                                                              Text('ETH',
                                                              style: GoogleFonts.montserrat(
                                                                textStyle:const TextStyle(
                                                                  fontWeight: FontWeight.w700,
                                                                  fontSize: 12,
                                                                  color: Color.fromRGBO(0, 0, 0, 1)
                                                                )
                                                              ),
                                                              )
                                                            ],
                                                          ),
                                    Padding(
                                      padding: const EdgeInsets.only(left: 30),
                                      child: Text("~\$30",
                                       style: GoogleFonts.montserratAlternates(
                                               textStyle:const TextStyle(
                                              fontWeight: FontWeight.w500,
                                                  fontSize: 9,
                                                   color: Color.fromRGBO(0, 0, 0, 1)
                                                                  )
                                                                ),
                                      ),
                                    )
                                                         ],
                                                     ),
                                                ),
                                                 SvgPicture.asset('assets/svg/mint.svg',height: 40,),
                                              ],
                                            ),
                                          ),
                                        )
                                      ],
                                    ),
                                  )
                                ],
                              ),
                             )
                          ],
                        )
                       ),
                    ),
                  ),
                );
              }),
          ),
        ],
      ),
    bottomNavigationBar: BottomNavigationBar(
      items: [
      BottomNavigationBarItem(icon: _naviIcon('assets/svg/homeicon.svg', 'Home',(){
        Navigator.of(context).push(MaterialPageRoute(builder: ((context) => const MyHomePage())));
      }),label:'',),
      BottomNavigationBarItem(icon:  _naviIcon('assets/svg/albumicon.svg', 'Album',(){
                Navigator.of(context).push(MaterialPageRoute(builder: ((context) => const AlbumPage())));
      }),label: ''),
      BottomNavigationBarItem(icon: _naviIcon('assets/svg/minticon.svg', 'Mint',(){}),label: '',),
      BottomNavigationBarItem(icon: _naviIcon('assets/svg/walleticon.svg', 'Wallet',(){}),label:'')
    ]),
  
        );
  
  }
}

Widget _naviIcon(String icon, String name,Function() onTap){
  return GestureDetector(
    onTap:onTap,
    child: SizedBox(
      child: Column(children: [
        SvgPicture.asset(icon),
        const SizedBox(height: 4,),
        Text(name,
        style: GoogleFonts.montserratAlternates(
          textStyle: const TextStyle(
            fontSize: 8.9,
             fontWeight: FontWeight.w500,
            color: Color.fromRGBO(0, 0, 0, 1)
            
          )
        ),
        )
      ],)
    ),
  );
}

Well, you are initializing twice, the second time => await _initializeAppKitModal(); // Fallback: initialize if not done yet shouldn't be needed. Please take a look at the example app inside modal/ folder

That being said, I'll change to registerSingletonIfAbsent inside the SDK but again, your second initialization shouldn't be needed.

I am still getting the same issue and when I navigate out of the page and back, I get this error Field '_appKitModal@44472063' has not been initialized.
E/flutter ( 5090): [ERROR: flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Type IMagicService is already registered inside GetIt.