Skip to content

Null safety Migration #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 19, 2021
Merged
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.4.32'
repositories {
google()
mavenCentral()
Expand Down
16 changes: 6 additions & 10 deletions lib/common/themes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

final ThemeData darkTheme = ThemeData(
floatingActionButtonTheme: const FloatingActionButtonThemeData(
floatingActionButtonTheme: const FloatingActionButtonThemeData(
backgroundColor: blueButton,

),
primarySwatch: Colors.grey,
textTheme: Typography(platform: defaultTargetPlatform).white,
primaryColor: Colors.white,
primaryTextTheme: TextTheme(
headline6: GoogleFonts.muli(color: Colors.white),
headline6: GoogleFonts.lato(color: Colors.white),
),
appBarTheme: const AppBarTheme(
backgroundColor: Color.fromRGBO(18, 19, 33, 1),
Expand All @@ -26,7 +25,7 @@ final ThemeData darkTheme = ThemeData(
scaffoldBackgroundColor: const Color.fromRGBO(18, 19, 33, 1),
colorScheme: ColorScheme.light(
primary: Colors.white,
secondary: Colors.grey[900],
secondary: Colors.grey[900]!,
onSecondary: blueButton),
// accentIconTheme: const IconThemeData(color: Colors.black),
dividerColor: Colors.transparent,
Expand All @@ -41,8 +40,6 @@ final ThemeData darkTheme = ThemeData(
final ThemeData lightTheme = ThemeData(
floatingActionButtonTheme: const FloatingActionButtonThemeData(
backgroundColor: blueButton,


),
textTheme: Typography(platform: defaultTargetPlatform).black,
appBarTheme: const AppBarTheme(
Expand All @@ -58,15 +55,14 @@ final ThemeData lightTheme = ThemeData(
// ),
),
primaryTextTheme: TextTheme(
headline6: GoogleFonts.muli(color: Colors.black),
headline6: GoogleFonts.lato(color: Colors.black),
),
primarySwatch: Colors.grey,
primaryColor: Colors.black,
brightness: Brightness.light,
backgroundColor: const Color(0xFFE5E5E5),
scaffoldBackgroundColor: const Color(0xFFE5E5E5),
colorScheme: const ColorScheme.dark(

brightness: Brightness.light,
primary: Colors.black,
secondary: Colors.grey,
Expand All @@ -84,11 +80,11 @@ final ThemeData lightTheme = ThemeData(
ThemeData bookTheme() {
TextTheme _bookTextTheme(TextTheme base) {
return base.copyWith(
headline4: base.headline4.copyWith(
headline4: base.headline4!.copyWith(
fontFamily: 'Poppins',
fontSize: 36.0,
color: const Color.fromRGBO(24, 25, 38, 1)),
bodyText1: base.bodyText1.copyWith(
bodyText1: base.bodyText1!.copyWith(
fontFamily: 'Poppins',
fontSize: 16.0,
color: Colors.white10,
Expand Down
23 changes: 14 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:books_app/common/themes.dart';
import 'package:books_app/constants/colors.dart';
import 'package:books_app/providers/books.dart';
import 'package:books_app/providers/quote.dart';
import 'package:books_app/providers/theme.dart';
import 'package:books_app/screens/home.dart';
import 'package:books_app/screens/initial_screen.dart';
Expand Down Expand Up @@ -28,6 +29,9 @@ Future<void> main() async {
ChangeNotifierProvider<FirebaseAuthService>(
create: (_) => FirebaseAuthService(),
),
ChangeNotifierProvider<QuoteService>(
create: (_) => QuoteService(),
)
],
child: MyApp(),
),
Expand All @@ -39,12 +43,13 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
// final dynamic myAppUser = FirebaseAuthService().currentUserFromFireBase;
return Listener(
onPointerUp: (_) {
final FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
currentFocus.focusedChild.unfocus();
}
},
onPointerUp: (_) {
final FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus &&
currentFocus.focusedChild != null) {
currentFocus.focusedChild!.unfocus();
}
},
child: MaterialApp(
theme: Provider.of<ThemeNotifier>(context).getTheme(),
debugShowCheckedModeBanner: false,
Expand All @@ -61,11 +66,11 @@ class Wrapper extends StatelessWidget {
Widget build(BuildContext context) {
final FirebaseAuthService firebaseAuthService =
Provider.of<FirebaseAuthService>(context);
return StreamBuilder<User>(
return StreamBuilder<User?>(
stream: firebaseAuthService.onAuthStateChanged,
builder: (_, AsyncSnapshot<User> snapshot) {
builder: (_, AsyncSnapshot<User?> snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
final User user = snapshot.data;
final User? user = snapshot.data;
return user == null ? InitialScreen() : Home();
} else {
return const Center(
Expand Down
22 changes: 11 additions & 11 deletions lib/providers/book.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import 'package:flutter/cupertino.dart';

class Book with ChangeNotifier {
// final String id;
final String isbn;
final String title;
final String author;
final String imageUrl;
final String userid;
final String? isbn;
final String? title;
final String? author;
final String? imageUrl;
final String? userid;
double rating;
final String description;
final String genre;
bool isBookMarked;
bool isOwned;
final String? description;
final String? genre;
bool? isBookMarked;
bool? isOwned;
bool isLent;
bool isBorrowed;
final int pages;
final String infoLink;
final String? infoLink;

Book({
// this.id,
Expand All @@ -37,7 +37,7 @@ class Book with ChangeNotifier {
});
//change isBookMarkedStatus
void changeBookMark() {
isBookMarked = !isBookMarked;
isBookMarked = !isBookMarked!;
notifyListeners();
}
}
51 changes: 26 additions & 25 deletions lib/providers/books.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ class Books with ChangeNotifier {
_savedBooks = <Book>[];
print('Getter SavedBooks called');
for (final Book book in _recommendedBooks) {
if (book.isBookMarked) {
if (book.isBookMarked!) {
_savedBooks.insert(0, book);
print('${book.title} Book Inserted in SavedBook List');
}
}
for (final Book book in _discoverNew) {
if (book.isBookMarked) {
if (book.isBookMarked!) {
_savedBooks.insert(0, book);
print('${book.title} Book Inserted in SavedBook List');
}
Expand All @@ -81,7 +81,8 @@ class Books with ChangeNotifier {
'totalItems': 0
};
try {
final http.Response response = await http.get(url + isbn.trim());
final http.Response response =
await http.get(Uri.parse(url + isbn.trim()));
final dynamic result = jsonDecode(response.body);
// print("Result From get Books From ISBN:");
// print(result["items"][0]);
Expand All @@ -95,7 +96,7 @@ class Books with ChangeNotifier {
Future<dynamic> getISBNFromName(String title) async {
const String url = 'https://www.googleapis.com/books/v1/volumes?q=';
try {
final http.Response response = await http.get(url + title);
final http.Response response = await http.get(Uri.parse(url + title));
final dynamic resultJson = jsonDecode(response.body);
if (resultJson != null) {
final String isbn = resultJson['items'][0]['volumeInfo']
Expand All @@ -109,8 +110,8 @@ class Books with ChangeNotifier {
}
}

Book makeBook(dynamic result) {
Book book;
Book? makeBook(dynamic result) {
Book? book;

if (result != null) {
final String title = result['volumeInfo']['title'].toString();
Expand Down Expand Up @@ -150,27 +151,27 @@ class Books with ChangeNotifier {
Book makeBookforDB(dynamic result, String isbnCode, String inputAuthor) {
// print(result);
Book book;
final String description =
result['items'][0]['volumeInfo']['description'] as String;
final String? description =
result['items'][0]['volumeInfo']['description'] as String?;
final String isbn = isbnCode;
final String infoLink =
result['items'][0]['volumeInfo']['infoLink'] as String;
final int pages = result['items'][0]['volumeInfo']['pageCount'] as int;
String imageLink, title, author;
final String? infoLink =
result['items'][0]['volumeInfo']['infoLink'] as String?;
final int? pages = result['items'][0]['volumeInfo']['pageCount'] as int?;
String? imageLink, title, author;
try {
title = result['items'][0]['volumeInfo']['title'] as String;
author = result['items'][0]['volumeInfo']['authors'][0] as String;
imageLink =
result['items'][0]['volumeInfo']['imageLinks']['thumbnail'] as String;
imageLink = imageLink.replaceFirst('http', 'https', 0);
title = result['items'][0]['volumeInfo']['title'] as String?;
author = result['items'][0]['volumeInfo']['authors'][0] as String?;
imageLink = result['items'][0]['volumeInfo']['imageLinks']['thumbnail']
as String?;
imageLink = imageLink!.replaceFirst('http', 'https', 0);
} catch (e) {
imageLink =
'https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/No-Image-Placeholder.svg/1200px-No-Image-Placeholder.svg.png';
author = inputAuthor;
print('imageLink is empty');
}

print(FirebaseAuth.instance.currentUser.uid);
print(FirebaseAuth.instance.currentUser!.uid);
print('ISBN' + isbn.toString());
print('Title:' + title.toString());
print('Author:' + author.toString());
Expand All @@ -190,18 +191,18 @@ class Books with ChangeNotifier {

Future<dynamic> topBooks() async {
const String recommendedURL =
'https://www.googleapis.com/books/v1/volumes?q=isbn';
'https://www.googleapis.com/books/v1/volumes?q=isbn:';

try {
final http.Response response = await http.get(Uri.parse(recommendedURL));
final dynamic result = jsonDecode(response.body);
print('result from Google API topBook func is $result');
final List list = result['items'] as List;
if (response != null) {
final dynamic result = jsonDecode(response.body);
print('result from Google API topBook func is $result');
final List? list = result['items'] as List?;

if (result != null) {
final List<Book> recommendedBooks = <Book>[];
final List<Book?> recommendedBooks = <Book?>[];

for (dynamic value in list) {
for (dynamic value in list!) {
recommendedBooks.add(makeBook(value));
}
return recommendedBooks;
Expand Down
22 changes: 22 additions & 0 deletions lib/providers/quote.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:books_app/services/base/base_services.dart';
import 'package:flutter/material.dart';

class QuoteService extends ChangeNotifier {
final String apiUrl =
'https://api.quotable.io/random?tags=inspirational,famous-quotes';
String content = 'Once we accept our limits, we go beyond them.';
String author = '- Albert Einstein'; //default quote

Future<void> getQuote() async {
try {
final BaseServices baseServices = BaseServices();
final Map<String, dynamic>? response = await baseServices.getAPI(apiUrl);
content = response!['content'].toString();
author = response['author'].toString();
print(content + ' ' + author);
notifyListeners();
} catch (e) {
print(e);
}
}
}
34 changes: 18 additions & 16 deletions lib/providers/user.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
class UserData {
String uid;
String displayName;
String email;
bool emailVerified;
String refreshToken;
bool isAnonymous;
String phoneNumber;
String photoURL;
String city;
String state;
double latitude;
double longitude;
double locationRange;
String countryName;
Map<String, dynamic> preferences;
import 'package:flutter/cupertino.dart';

class UserData extends ChangeNotifier {
String? uid;
String? displayName;
String? email;
bool? emailVerified;
String? refreshToken;
bool? isAnonymous;
String? phoneNumber;
String? photoURL;
String? city;
String? state;
double? latitude;
double? longitude;
double? locationRange;
String? countryName;
Map<String, dynamic>? preferences;
UserData(
{this.uid,
this.countryName,
Expand Down
10 changes: 5 additions & 5 deletions lib/screens/add_book.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';

class AddBook extends StatefulWidget {
const AddBook({Key key}) : super(key: key);
const AddBook({Key? key}) : super(key: key);
@override
_AddBookState createState() => _AddBookState();
}
Expand Down Expand Up @@ -107,8 +107,8 @@ class _AddBookState extends State<AddBook> {
horizontal: 15, vertical: 3),
child: TextFormField(
controller: _isbnCode,
validator: (String value) {
if (value.isEmpty) {
validator: (String? value) {
if (value!.isEmpty) {
return 'Please enter a valid ISBN value';
}
if (value.length < 10 || value.length > 13) {
Expand Down Expand Up @@ -148,8 +148,8 @@ class _AddBookState extends State<AddBook> {
name: 'Add your Book',
// color: blackButton,
myFunction: () async {
if (_bookKey.currentState.validate()) {
_bookKey.currentState.save();
if (_bookKey.currentState!.validate()) {
_bookKey.currentState!.save();
final dynamic result =
await bookList.getBooksbyISBN(_isbnCode.text);
if (result['totalItems'] != 0) {
Expand Down
Loading