Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
I was trying to display images from my device storage into a gridview as you did in your FlutterGallery App. My current code look like this.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:wallpapers_app/display.dart';
import 'package:storage_path/storage_path.dart';
import 'package:wallpapers_app/image_model.dart';
class Downloads extends StatefulWidget {
int width;
int height;
Downloads(this.width, this.height);
@override
_DownloadsState createState() => _DownloadsState();
}
class _DownloadsState extends State<Downloads>
with AutomaticKeepAliveClientMixin {
final Color loadingTextColor = Color(0xFF000000);
final Color bgColor = Color(0xFFFFFFFF);
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
List data = [];
@override
Widget build(BuildContext context) {
ScreenUtil.init(context, width: 720, height: 1440, allowFontScaling: true);
return FutureBuilder(
future: StoragePath.imagesPath,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List<dynamic> data = json.decode(snapshot.data);
print("Done");
return new Container(
color: bgColor,
child: Scrollbar(
child: GridView.builder(
shrinkWrap: true,
padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
itemCount: data.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, childAspectRatio: 0.75),
itemBuilder: (BuildContext context, int index) {
ImageModel imageModel =
ImageModel.fromJson(data[index]);
return new GestureDetector(
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Positioned.fill(
child: new Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(24))),
elevation: 0.0,
semanticContainer: true,
margin: EdgeInsets.fromLTRB(4, 4, 4, 4),
clipBehavior: Clip.antiAliasWithSaveLayer,
child: new Container(
child: new Hero(
tag: data[index]["url"],
child: Image(
image: FileImage(
File(imageModel.files[0]),
),
fit: BoxFit.cover,
))),
),
),
],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return Display(
data[index]["url"],
data[index]["thumb"],
data[index]["color"],
data[index]["color2"],
data[index]["views"],
data[index]["resolution"],
"https://whvn.cc/${data[index]["id"]}",
data[index]["created"],
data[index]["fav"]);
},
),
);
},
);
}),
));
}
return Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image(
image: AssetImage("assets/images/loading.png"),
height: 600.h,
width: 600.w,
),
Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
"Loading",
style: GoogleFonts.raleway(
fontSize: 30, color: loadingTextColor),
textAlign: TextAlign.center,
),
),
Text(
"Sit Back and wait a few seconds\nas your downloaded wallpapers are\nloading.",
style: GoogleFonts.raleway(
fontSize: 16, color: loadingTextColor),
textAlign: TextAlign.center,
),
],
),
),
);
});
}
@override
bool get wantKeepAlive => true;
}
I also have image_model.dart in my lib folder.
List<String> files;
String folderName;
ImageModel({this.files, this.folderName});
ImageModel.fromJson(Map<String, dynamic> json) {
files = json['files'].cast<String>();
folderName = json['folderName'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['files'] = this.files;
data['folderName'] = this.folderName;
return data;
}
}
I have also added external storage read and write permissions in my AndroidManifest.xml, and I have also allowed storage permission on my device, still this error occurs whenever the above class is called. Error -
E/MethodChannel#storage_path(21719): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
E/MethodChannel#storage_path(21719): at com.follow2vivek.storagepath.StoragePathPlugin.getImagePaths(StoragePathPlugin.java:130)
E/MethodChannel#storage_path(21719): at com.follow2vivek.storagepath.StoragePathPlugin.access$000(StoragePathPlugin.java:31)
E/MethodChannel#storage_path(21719): at com.follow2vivek.storagepath.StoragePathPlugin$1.onGranted(StoragePathPlugin.java:56)
E/MethodChannel#storage_path(21719): at com.nabinbhandari.android.permissions.Permissions.check(Permissions.java:105)
E/MethodChannel#storage_path(21719): at com.nabinbhandari.android.permissions.Permissions.check(Permissions.java:52)
E/MethodChannel#storage_path(21719): at com.follow2vivek.storagepath.StoragePathPlugin.onMethodCall(StoragePathPlugin.java:53)
E/MethodChannel#storage_path(21719): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
E/MethodChannel#storage_path(21719): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/MethodChannel#storage_path(21719): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/MethodChannel#storage_path(21719): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#storage_path(21719): at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#storage_path(21719): at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#storage_path(21719): at android.app.ActivityThread.main(ActivityThread.java:7397)
E/MethodChannel#storage_path(21719): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#storage_path(21719): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#storage_path(21719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)
E/AccessibilityBridge(21719): VirtualView node must not be the root node.
W/System (21719): A resource failed to call close.
The output of flutter doctor on my system is -
[✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Linux, locale en_IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[!] Android Studio (version 3.6)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] Connected device (1 available)
! Doctor found issues in 1 category.
I am using VS Code as my editor which my flutter doctor doesn't show for some reason. Please help with this issue.
There is an issue with the Native side coding. I didn't dig deep into the issue
open the file com.follow2vivek.storagepath.StoragePathPlugin.java and replace the line no 130 to 140 with the below lines
` try{
if(filesModelArrayList.get(i).getFolder().equals(cursor.getString(column_index_folder_name))) {
hasFolder = true;
position = i;
break;
} else {
hasFolder = false;
}
} catch (Exception e) {
}`
This file will be inside the /Flutter/flutter/.pub-cache/hosted/pub.dartlang.org
This is temporary fix.