I have a Flutter app on version 2.2.3 into which I recently integrated a native SDK using the Platform channel method. For some reason after doing this, my Flutter app started showing a black screen whenever I run it either on the emulator or on an actual device. The app gets stuck on the black screen and does not proceed to showing the Home Screen. Here is my code for the main file:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
InAppPurchaseAndroidPlatformAddition.enablePendingPurchases();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIOverlays([]);
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
print('Inside main.dart build method');
return FutureBuilder(
future: _initialization,
builder: (context2, snapshot) {
if (snapshot.hasError) {
NetworkErr();
return Container();
} else if (snapshot.connectionState == ConnectionState.done) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (ctx) => ClueList(),
),
ChangeNotifierProvider(
create: (ctx) => Score(),
),
ChangeNotifierProvider(
create: (ctx) => Auth(),
),
ChangeNotifierProvider(
create: (ctx) => Badges(),
),
],
child: MaterialApp(
theme: ThemeData(
primarySwatch: Colors.teal,
// primaryColor: Color.fromARGB(150, 35, 152, 159,),
primaryColorLight: Color.fromARGB(
80,
35,
152,
159,
),
accentColor: Color.fromARGB(255, 255, 185, 36),
textTheme: ThemeData.light().textTheme.copyWith(
bodyText2: TextStyle(
fontFamily: 'Happy School',
fontSize: 20,
))),
home: HomeScreen(),
In the MainActivity.java file I have integrated the native sdk wherein I make a call to retrieve ads from Yodo1 MAS Below is the code from MainActivity.java file.
package com.example.xxxxx;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import com.yodo1.mas.Yodo1Mas;
import com.yodo1.mas.error.Yodo1MasError;
import com.yodo1.mas.event.Yodo1MasAdEvent;
import io.flutter.app.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("before Calling MAS Get Instance");
Yodo1Mas.getInstance().init(this, "xxxxxxxxx", new Yodo1Mas.InitListener() {
@Override
public void onMasInitSuccessful() {
System.out.println("MAS Init Successful");
}
@Override
public void onMasInitFailed(@NonNull Yodo1MasError error) {
}
});
new MethodChannel(getFlutterView(), "com.examples.xxxxxx/yodo1").setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getBannerAd")) {
System.out.println("RADHA LOading Banner Ad");
getBannerAd();
}
}
}
);
}
private void getBannerAd() {
int align = Yodo1Mas.BannerTop | Yodo1Mas.BannerHorizontalCenter;
boolean isLoaded = Yodo1Mas.getInstance().isBannerAdLoaded();
if (isLoaded) {
Yodo1Mas.getInstance().showBannerAd(MainActivity.this, align);
Yodo1Mas.getInstance().setBannerListener(new Yodo1Mas.BannerListener() {
@Override
public void onAdOpened(@NonNull Yodo1MasAdEvent event) {
}
@Override
public void onAdError(@NonNull Yodo1MasAdEvent event, @NonNull Yodo1MasError error) {
}
@Override
public void onAdClosed(@NonNull Yodo1MasAdEvent event) {
}
}
);
}
}
}
This is my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxxx">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="false"
android:networkSecurityConfig="@xml/network_security_config">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxx"/>
<!-- <meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" /> -->
<!-- <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" /> -->
</application>
</manifest>
I also tried the following:
- I ran
flutter pub cache repair
- My original project was a Flutter + Kotlin project. Since the code to integrate the native sdk was only available in Java, I converted the project into a Java project by moving the folder structure from src/main/kotlin to src/main/java as explained here : Convert an existing Flutter Kotlin project to Flutter Java project
I've been stuck with this for over 3 days now. I searched almost all the solutions I found on SOF for the black screen but to no avail. If you have any other suggestions please let me know. Thanks.