Categories
discuss

Requested flutter engine does not exist in Add to app

I have flutter into the existing native app (Add to app) with a cached flutter engine.

override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(TAG, "onCreate")
        
        
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)
        context = this.application.applicationContext
        if (!FlutterEngineCache.getInstance().contains(
                FLUTTER_ENGINE
            )
        ) {
            mFlutterEngine = FlutterEngine(context)
            mFlutterEngine.dartExecutor
                .executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
            FlutterEngineCache.getInstance()
                .put(FLUTTER_ENGINE, mFlutterEngine)
        }
        //Create Flutter Fragment
        mFragmentManager = supportFragmentManager
        mFlutterFragment = mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT) as FlutterFragment?
        if (mFlutterFragment == null) {
            mFlutterFragment =
                FlutterFragment.withCachedEngine(FLUTTER_ENGINE).transparencyMode(FlutterView.TransparencyMode.opaque).build()
            mFragmentManager
                .beginTransaction()
                .add(R.id.fragment_container, mFlutterFragment as Fragment, FLUTTER_FRAGMENT)
                .commit()
        } else {
            mFragmentManager
                .beginTransaction()
                .show(mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT)!!)
                .commit()
        }
}

I am getting the following crash in the production app

  Caused by: java.lang.IllegalStateException: The requested cached FlutterEngine did not exist in the FlutterEngineCache: 'FLUTTER_ENGINE'
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.setupFlutterEngine(FlutterActivityAndFragmentDelegate.java:211)
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:157)
        at io.flutter.embedding.android.FlutterFragment.onAttach(FlutterFragment.java:597)
        at androidx.fragment.app.Fragment.performAttach(Fragment.java:2574)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:828)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
        at androidx.fragment.app.FragmentManagerImpl.dispatchCreate(FragmentManagerImpl.java:2607)
        at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:235)
        at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:316)
        at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)
        at com.flutter.android.MainActivity.onCreate(MainActivity.kt:33)
 

Answer

How to reproduce

  1. Open developer settings, set “Background processes” to “No Background Processes” and set on the Don’t keep activities.

  2. Open the app press and home

  3. Open the app again .. it will crash with the exception.

    Caused by: java.lang.IllegalStateException: The requested cached FlutterEngine did not exist in the FlutterEngineCache: ‘FLUTTER_ENGINE’

Solution

initialize the flutter engine before super.oncreate()

override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(TAG, "onCreate")
         context = this.application.applicationContext
        if (!FlutterEngineCache.getInstance().contains(
                FLUTTER_ENGINE
            )
        ) {
            mFlutterEngine = FlutterEngine(context)
            mFlutterEngine.dartExecutor
                .executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
            FlutterEngineCache.getInstance()
                .put(FLUTTER_ENGINE, mFlutterEngine)
        }
       
        
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)
        //Create Flutter Fragment
        mFragmentManager = supportFragmentManager
        mFlutterFragment = mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT) as FlutterFragment?
        if (mFlutterFragment == null) {
            mFlutterFragment =
                FlutterFragment.withCachedEngine(FLUTTER_ENGINE).transparencyMode(FlutterView.TransparencyMode.opaque).build()
            mFragmentManager
                .beginTransaction()
                .add(R.id.fragment_container, mFlutterFragment as Fragment, FLUTTER_FRAGMENT)
                .commit()
        } else {
            mFragmentManager
                .beginTransaction()
                .show(mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT)!!)
                .commit()
        }
Categories
discuss

ESLint rule to block users from disabling ESLint rules

I have a project with very few ESLint rules that are mandatory for all the collaborators, unfortunately, I’ve found some developers have disabled some rules with no justification and the Code Review failed to stop this. Is there a way to configure ESLint to ignore comments disabling rules or for it to throw warnings whenever this happens?

I’ve been looking for a while and haven’t found anything of the sort. Is there maybe another way to do this?

Answer

Wow, very cool scenario. But I feel sorry for the developers who might want to kick me for answering this. Follow the below steps

npm install --save-dev eslint eslint-plugin-eslint-comments

in your .eslintrc.* file add

{
    "extends": [
        "eslint:recommended",
        "plugin:eslint-comments/recommended"
    ],
    "rules": {
        "eslint-comments/no-use": ["error", {"allow": []}]
    }
}

For more info check this link https://mysticatea.github.io/eslint-plugin-eslint-comments/rules/no-use.html

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..