Browse Source

修复空安全

boyrobot 1 year ago
parent
commit
b2f6a0abe0

+ 4 - 4
lib/model/product.dart

@@ -1,10 +1,10 @@
 import 'package:flutter/material.dart';
 
 class Product {
-  final String image, title, description;
-  final int price, size, id;
-  final Color color;
-  bool status;
+  final String? image, title, description;
+  final int? price, size, id;
+  final Color? color;
+  bool? status;
 
   Product(
       {this.id,

+ 1 - 1
lib/model/table.dart

@@ -20,7 +20,7 @@ class TableStatusList with ChangeNotifier {
     notifyListeners();
   }
 
-  deletetable(int index) {
+  deletetable(int? index) {
     print("[删除桌号]" + index.toString());
     _tableStatusMap.remove(index);
     notifyListeners();

+ 22 - 17
lib/model/tableDetail.dart

@@ -1,11 +1,12 @@
+import 'package:collection/collection.dart' show IterableExtension;
 import 'package:flutter/cupertino.dart';
 import 'package:fooddeliveryapp/model/product.dart';
 
 class Item {
-  int count;
-  Product product;
+  int? count;
+  Product? product;
 
-  Item(int i, Product p) {
+  Item(int i, Product? p) {
     this.count = i;
     product = p;
   }
@@ -13,11 +14,11 @@ class Item {
 
 class TableDetail with ChangeNotifier {
   //记录当前使用台号
-  int _tableId;
+  int? _tableId;
 
-  DateTime openTime;
+  DateTime? openTime;
 
-  TableDetail({int id}) {
+  TableDetail({int? id}) {
     this._tableId = id;
     notifyListeners();
   }
@@ -26,9 +27,9 @@ class TableDetail with ChangeNotifier {
 
   List<Item> get items => _items;
 
-  int getTableId() => _tableId;
+  int? getTableId() => _tableId;
 
-  DateTime getOpenTime() => openTime;
+  DateTime? getOpenTime() => openTime;
 
   setTableId(int id) {
     print("[tabledetails]设置桌号id" + id.toString());
@@ -37,23 +38,27 @@ class TableDetail with ChangeNotifier {
     notifyListeners();
   }
 
-  addItem(Product p) {
-    Item item = _items.firstWhere((i) => i.product == p, orElse: () => null);
+  addItem(Product? p) {
+    Item? item = _items.firstWhereOrNull((i) => i.product == p);
     if (item == null) {
       item = Item(0, p);
       _items.add(item);
     }
-    item.count++;
+    if (item.count != null) {
+      item.count = item.count! + 1;
+    }
     notifyListeners();
   }
 
   // 从购物车移除商品,判断数量,通知
-  removeItem(Product p) {
-    Item item = _items.firstWhere((i) => i.product == p, orElse: () => null);
+  removeItem(Product? p) {
+    Item? item = _items.firstWhereOrNull((i) => i.product == p);
     if (item == null) {
       return;
     }
-    item.count--;
+    if (item.count != null) {
+      item.count = item.count! - 1;
+    }
     if (item.count == 0) {
       _items.remove(item);
     }
@@ -65,8 +70,8 @@ class TableDetail with ChangeNotifier {
   }
 
   // 获取购物车中指定商品数量,不存在返回0
-  int getItemCount(Product p) {
-    Item item = _items.firstWhere((i) => i.product == p, orElse: () => null);
+  int? getItemCount(Product? p) {
+    Item? item = _items.firstWhereOrNull((i) => i.product == p);
     return item == null ? 0 : item.count;
   }
 
@@ -74,7 +79,7 @@ class TableDetail with ChangeNotifier {
   String getTotalPrices() {
     double total = 0;
     for (var item in _items) {
-      total += item.product.price * item.count;
+      total += item.product!.price! * item.count!;
     }
     return total.toStringAsFixed(2);
   }

+ 5 - 6
lib/model/user.dart

@@ -5,16 +5,16 @@ import 'package:flutter/material.dart';
 //用户登录状态
 class UserStatus with ChangeNotifier {
   bool _loginStatus = false;
-  String _userName;
-  int _performance;
+  String? _userName;
+  int? _performance;
 
-  get value => _loginStatus;
+  bool get value => _loginStatus;
 
-  get name => _userName;
+ String? get name => _userName;
 
   get perfromace => _performance;
 
-  set name(String name) {
+  set name(String? name) {
     _userName = name;
     notifyListeners();
   }
@@ -24,4 +24,3 @@ class UserStatus with ChangeNotifier {
     notifyListeners();
   }
 }
-

+ 8 - 9
lib/pages/cart.dart

@@ -1,4 +1,3 @@
-import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_svg/svg.dart';
 import 'package:fooddeliveryapp/model/constants.dart';
@@ -12,7 +11,7 @@ class OrderCart extends StatelessWidget {
   static const routeName = "/cart";
   @override
   Widget build(BuildContext context) {
-    int index = ModalRoute.of(context).settings.arguments;
+    int? index = ModalRoute.of(context)!.settings.arguments as int?;
     return Scaffold(
       // appBar: AppBar(
       //   title: Text('购物车'),
@@ -26,9 +25,9 @@ class OrderCart extends StatelessWidget {
 }
 
 class ItemList extends StatelessWidget {
-  final int index;
+  final int? index;
 
-  const ItemList({Key key, this.index}) : super(key: key);
+  const ItemList({Key? key, this.index}) : super(key: key);
   @override
   Widget build(BuildContext context) {
     return Column(
@@ -51,7 +50,7 @@ class ItemList extends StatelessWidget {
 
 class CartList extends StatelessWidget {
   const CartList({
-    Key key,
+    Key? key,
   }) : super(key: key);
 
   @override
@@ -68,7 +67,7 @@ class CartList extends StatelessWidget {
                   child: Container(
                     constraints: BoxConstraints(maxHeight: kDefaultPaddin * 3),
                     child: Image.asset(
-                      cart.items[index].product.image,
+                      cart.items[index].product!.image!,
                       fit: BoxFit.contain,
                     ),
                   ),
@@ -76,19 +75,19 @@ class CartList extends StatelessWidget {
                 Expanded(
                   flex: 2,
                   child: Text(
-                    '${cart.items[index].product.title}',
+                    '${cart.items[index].product!.title}',
                     style: TextStyle(fontSize: 25),
                   ),
                 ),
                 Expanded(flex: 1, child: Text('数量:${cart.items[index].count}')),
                 Expanded(
                   flex: 1,
-                  child: cart.items[index].product.status == true
+                  child: cart.items[index].product!.status == true
                       ? Text("已制作")
                       : TextButton(
                           style: ButtonStyle(
                               backgroundColor: MaterialStateProperty.all(
-                                  cart.items[index].product.color)),
+                                  cart.items[index].product!.color)),
                           child: Text("退菜"),
                           onPressed: () {
                             print("退菜");

+ 5 - 5
lib/pages/check_out.dart

@@ -5,11 +5,11 @@ import 'package:fooddeliveryapp/model/tableDetail.dart';
 import 'package:provider/provider.dart';
 
 class CheckOut extends StatelessWidget {
-  final int index;
+  final int? index;
 
   static final routeName = "/checkout";
 
-  const CheckOut({Key key, this.index}) : super(key: key);
+  const CheckOut({Key? key, this.index}) : super(key: key);
 
   @override
   Widget build(BuildContext context) {
@@ -30,7 +30,7 @@ class CheckOut extends StatelessWidget {
               "订单支付页面",
               style: Theme.of(context)
                   .textTheme
-                  .headline4
+                  .headlineMedium!
                   .copyWith(color: Colors.black, fontWeight: FontWeight.normal),
             ),
             Container(
@@ -46,7 +46,7 @@ class CheckOut extends StatelessWidget {
             Text(
               "开台时间:" +
                   myTableDetail
-                      .getOpenTime()
+                      .getOpenTime()!
                       .toIso8601String()
                       .split("T")[1]
                       .substring(0, 8),
@@ -85,7 +85,7 @@ class CheckOut extends StatelessWidget {
 
 class TotalPrices extends StatelessWidget {
   const TotalPrices({
-    Key key,
+    Key? key,
   }) : super(key: key);
 
   @override

+ 4 - 4
lib/pages/details/components/add_to_cart.dart

@@ -2,17 +2,17 @@ import 'package:flutter/material.dart';
 import 'package:fooddeliveryapp/model/constants.dart';
 
 class AddToCart extends StatelessWidget {
-  final Function pressButton;
+  final Function? pressButton;
 
   const AddToCart({
-    Key key,
+    Key? key,
     // @required this.product,
     this.count,
     this.pressButton,
   }) : super(key: key);
 
   // final Product product;
-  final int count;
+  final int? count;
 
   @override
   Widget build(BuildContext context) {
@@ -31,7 +31,7 @@ class AddToCart extends StatelessWidget {
                 onPressed: () {
                   ///加入购物车
                   print("数量:" + count.toString());
-                  pressButton();
+                  pressButton!();
                 },
                 child: Text(
                   "结账".toUpperCase(),

+ 2 - 2
lib/pages/details/components/body.dart

@@ -6,9 +6,9 @@ import 'description.dart';
 import 'product_title_with_image.dart';
 
 class DetailsBody extends StatelessWidget {
-  final Product product;
+  final Product? product;
 
-  const DetailsBody({Key key, this.product}) : super(key: key);
+  const DetailsBody({Key? key, this.product}) : super(key: key);
   @override
   Widget build(BuildContext context) {
     // It provide us total height and width

+ 17 - 13
lib/pages/details/components/cart_counter.dart

@@ -5,34 +5,36 @@ import 'package:fooddeliveryapp/model/tableDetail.dart';
 import 'package:provider/provider.dart';
 
 class CartCounter extends StatefulWidget {
-  final Product product;
+  final Product? product;
   const CartCounter({
-    Key key,
-    @required this.product,
+    Key? key,
+    required this.product,
   }) : super(key: key);
   @override
   _CartCounterState createState() => _CartCounterState(product);
 }
 
 class _CartCounterState extends State<CartCounter> {
-  final Product product;
+  final Product? product;
 
   _CartCounterState(this.product);
   @override
   Widget build(BuildContext context) {
     final myTableDetail = Provider.of<TableDetail>(context);
-    int numOfItems = myTableDetail.getItemCount(product);
+    int? numOfItems = myTableDetail.getItemCount(product);
     return Row(
       children: [
         buildOutlineButton(
           icon: Icons.remove,
           press: () {
-            if (numOfItems > 0) {
+            if (numOfItems! > 0) {
               setState(() {
-                numOfItems--;
+                if (numOfItems != null) {
+                  numOfItems = numOfItems! - 1;
+                }
                 myTableDetail.removeItem(product);
                 print("[通知]减少商品" +
-                    product.id.toString() +
+                    product!.id.toString() +
                     "NUMS:" +
                     myTableDetail.getItemCount(product).toString());
               });
@@ -44,17 +46,19 @@ class _CartCounterState extends State<CartCounter> {
           child: Text(
             // if our item is less  then 10 then  it shows 01 02 like that
             numOfItems.toString().padLeft(2, "0"),
-            style: Theme.of(context).textTheme.headline6,
+            style: Theme.of(context).textTheme.titleLarge,
           ),
         ),
         buildOutlineButton(
             icon: Icons.add,
             press: () {
               setState(() {
-                numOfItems++;
+                if (numOfItems != null) {
+                  numOfItems = numOfItems! + 1;
+                }
                 myTableDetail.addItem(product);
                 print("[通知]添加商品" +
-                    product.id.toString() +
+                    product!.id.toString() +
                     "NUMS:" +
                     myTableDetail.getItemCount(product).toString());
               });
@@ -63,7 +67,7 @@ class _CartCounterState extends State<CartCounter> {
     );
   }
 
-  SizedBox buildOutlineButton({IconData icon, Function press}) {
+  SizedBox buildOutlineButton({IconData? icon, Function? press}) {
     return SizedBox(
       width: 40,
       height: 32,
@@ -72,7 +76,7 @@ class _CartCounterState extends State<CartCounter> {
             padding: MaterialStateProperty.all(EdgeInsets.zero),
             shape: MaterialStateProperty.all(RoundedRectangleBorder(
                 borderRadius: BorderRadius.circular(13)))),
-        onPressed: press,
+        onPressed: press as void Function()?,
         child: Icon(icon),
       ),
     );

+ 6 - 6
lib/pages/details/components/color_and_size.dart

@@ -4,8 +4,8 @@ import 'package:fooddeliveryapp/model/product.dart';
 
 class ColorAndSize extends StatelessWidget {
   const ColorAndSize({
-    Key key,
-    @required this.product,
+    Key? key,
+    required this.product,
   }) : super(key: key);
 
   final Product product;
@@ -42,7 +42,7 @@ class ColorAndSize extends StatelessWidget {
                   text: "${product.size} cm",
                   style: Theme.of(context)
                       .textTheme
-                      .headline5
+                      .headlineSmall!
                       .copyWith(fontWeight: FontWeight.bold),
                 )
               ],
@@ -55,10 +55,10 @@ class ColorAndSize extends StatelessWidget {
 }
 
 class ColorDot extends StatelessWidget {
-  final Color color;
+  final Color? color;
   final bool isSelected;
   const ColorDot({
-    Key key,
+    Key? key,
     this.color,
     // by default isSelected is false
     this.isSelected = false,
@@ -77,7 +77,7 @@ class ColorDot extends StatelessWidget {
       decoration: BoxDecoration(
         shape: BoxShape.circle,
         border: Border.all(
-          color: isSelected ? color : Colors.transparent,
+          color: isSelected ? color! : Colors.transparent,
         ),
       ),
       child: DecoratedBox(

+ 3 - 3
lib/pages/details/components/counter_with_fav_btn.dart

@@ -4,11 +4,11 @@ import 'package:fooddeliveryapp/model/product.dart';
 import 'cart_counter.dart';
 
 class CounterWithFavBtn extends StatelessWidget {
-  final Product product;
+  final Product? product;
   // final TableDetail myTableDetail;
   const CounterWithFavBtn({
-    Key key,
-    @required this.product,
+    Key? key,
+    required this.product,
     // @required this.myTableDetail,
   }) : super(key: key);
 

+ 4 - 4
lib/pages/details/components/description.dart

@@ -4,18 +4,18 @@ import 'package:fooddeliveryapp/model/product.dart';
 
 class Description extends StatelessWidget {
   const Description({
-    Key key,
-    @required this.product,
+    Key? key,
+    required this.product,
   }) : super(key: key);
 
-  final Product product;
+  final Product? product;
 
   @override
   Widget build(BuildContext context) {
     return Padding(
       padding: const EdgeInsets.symmetric(vertical: kDefaultPaddin),
       child: Text(
-        product.description,
+        product!.description!,
         style: TextStyle(height: 1.5),
       ),
     );

+ 9 - 9
lib/pages/details/components/product_title_with_image.dart

@@ -4,11 +4,11 @@ import 'package:fooddeliveryapp/model/product.dart';
 
 class ProductTitleWithImage extends StatelessWidget {
   const ProductTitleWithImage({
-    Key key,
-    @required this.product,
+    Key? key,
+    required this.product,
   }) : super(key: key);
 
-  final Product product;
+  final Product? product;
 
   @override
   Widget build(BuildContext context) {
@@ -22,10 +22,10 @@ class ProductTitleWithImage extends StatelessWidget {
             style: TextStyle(color: Colors.white),
           ),
           Text(
-            product.title,
+            product!.title!,
             style: Theme.of(context)
                 .textTheme
-                .headline4
+                .headlineMedium!
                 .copyWith(color: Colors.white, fontWeight: FontWeight.bold),
           ),
           SizedBox(height: kDefaultPaddin),
@@ -36,8 +36,8 @@ class ProductTitleWithImage extends StatelessWidget {
                   children: [
                     TextSpan(text: "价格\n"),
                     TextSpan(
-                      text: "${product.price}元",
-                      style: Theme.of(context).textTheme.headline4.copyWith(
+                      text: "${product!.price}元",
+                      style: Theme.of(context).textTheme.headlineMedium!.copyWith(
                           color: Colors.white, fontWeight: FontWeight.bold),
                     ),
                   ],
@@ -46,9 +46,9 @@ class ProductTitleWithImage extends StatelessWidget {
               SizedBox(width: kDefaultPaddin),
               Expanded(
                 child: Hero(
-                  tag: "${product.id}",
+                  tag: "${product!.id}",
                   child: Image.asset(
-                    product.image,
+                    product!.image!,
                     fit: BoxFit.fill,
                   ),
                 ),

+ 4 - 4
lib/pages/details/detail_screen.dart

@@ -6,15 +6,15 @@ import 'package:fooddeliveryapp/pages/cart.dart';
 import 'package:fooddeliveryapp/pages/details/components/body.dart';
 
 class DetailsScreen extends StatelessWidget {
-  final Product product;
+  final Product? product;
 
-  const DetailsScreen({Key key, this.product}) : super(key: key);
+  const DetailsScreen({Key? key, this.product}) : super(key: key);
 
   @override
   Widget build(BuildContext context) {
     return Scaffold(
       appBar: buildAppBar(context),
-      backgroundColor: product.color,
+      backgroundColor: product!.color,
       body: DetailsBody(
         product: product,
       ),
@@ -24,7 +24,7 @@ class DetailsScreen extends StatelessWidget {
   AppBar buildAppBar(BuildContext context) {
     return AppBar(
       elevation: 0,
-      backgroundColor: product.color,
+      backgroundColor: product!.color,
       leading: IconButton(
         icon: SvgPicture.asset(
           'assets/icons/back.svg',

+ 6 - 6
lib/pages/login_page.dart

@@ -15,8 +15,8 @@ class LoginPage extends StatefulWidget {
 }
 
 class _LoginPageState extends State<LoginPage> {
-  TextEditingController _pwdEditController;
-  TextEditingController _userNameEditController;
+  TextEditingController? _pwdEditController;
+  TextEditingController? _userNameEditController;
 
   final FocusNode _userNameFocusNode = FocusNode();
   final FocusNode _pwdFocusNode = FocusNode();
@@ -28,8 +28,8 @@ class _LoginPageState extends State<LoginPage> {
     _pwdEditController = TextEditingController();
     _userNameEditController = TextEditingController();
 
-    _pwdEditController.addListener(() => setState(() => {}));
-    _userNameEditController.addListener(() => setState(() => {}));
+    _pwdEditController!.addListener(() => setState(() => {}));
+    _userNameEditController!.addListener(() => setState(() => {}));
   }
 
   @override
@@ -243,7 +243,7 @@ class _LoginPageState extends State<LoginPage> {
   }
 
   bool checkInput() {
-    if (_userNameEditController.text.length == 0) {
+    if (_userNameEditController!.text.length == 0) {
       Fluttertoast.showToast(
           msg: "请输入用户名",
           gravity: ToastGravity.CENTER,
@@ -252,7 +252,7 @@ class _LoginPageState extends State<LoginPage> {
           fontSize: 14.0);
 
       return false;
-    } else if (_pwdEditController.text.length == 0) {
+    } else if (_pwdEditController!.text.length == 0) {
       Fluttertoast.showToast(
           msg: "请输入密码",
           gravity: ToastGravity.CENTER,

+ 2 - 2
lib/pages/order/components/body.dart

@@ -18,8 +18,8 @@ class Body extends StatelessWidget {
 
 class Menu extends StatelessWidget {
   const Menu({
-    Key key,
-    @required this.productsList,
+    Key? key,
+    required this.productsList,
   }) : super(key: key);
 
   final List<Product> productsList;

+ 9 - 9
lib/pages/order/components/item-card.dart

@@ -3,11 +3,11 @@ import 'package:fooddeliveryapp/model/constants.dart';
 import 'package:fooddeliveryapp/model/product.dart';
 
 class ItemCard extends StatelessWidget {
-  final Product product;
-  final Function press;
+  final Product? product;
+  final Function? press;
 
   const ItemCard({
-    Key key,
+    Key? key,
     this.product,
     this.press,
   }) : super(key: key);
@@ -15,7 +15,7 @@ class ItemCard extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return GestureDetector(
-      onTap: press,
+      onTap: press as void Function()?,
       child: Column(
         crossAxisAlignment: CrossAxisAlignment.start,
         children: [
@@ -27,12 +27,12 @@ class ItemCard extends StatelessWidget {
               // height: 180,
               // width: 160,
               decoration: BoxDecoration(
-                color: product.color,
+                color: product!.color,
                 borderRadius: BorderRadius.circular(16),
               ),
               child: Hero(
-                tag: "${product.id}",
-                child: Image.asset(product.image),
+                tag: "${product!.id}",
+                child: Image.asset(product!.image!),
               ),
             ),
           ),
@@ -40,12 +40,12 @@ class ItemCard extends StatelessWidget {
             padding: const EdgeInsets.symmetric(vertical: kDefaultPaddin / 4),
             child: Text(
               // products is out demo list
-              product.title,
+              product!.title!,
               style: TextStyle(color: kTextLightColor),
             ),
           ),
           Text(
-            "${product.price}元",
+            "${product!.price}元",
             style: TextStyle(fontWeight: FontWeight.bold),
           )
         ],

+ 2 - 2
lib/pages/order/order_home_screen.dart

@@ -10,7 +10,7 @@ class HomeSrcreen extends StatelessWidget {
   final int index;
 
   //构造函数 同时传入桌号
-  HomeSrcreen({Key key, @required this.index}) : super(key: key);
+  HomeSrcreen({Key? key, required this.index}) : super(key: key);
 
   @override
   Widget build(BuildContext context) {
@@ -33,7 +33,7 @@ class HomeSrcreen extends StatelessWidget {
           "海底捞火锅",
           style: Theme.of(context)
               .textTheme
-              .headline5
+              .headlineSmall!
               .copyWith(fontWeight: FontWeight.bold),
         ),
       ),

+ 2 - 2
pubspec.lock

@@ -42,7 +42,7 @@ packages:
     source: hosted
     version: "1.1.1"
   collection:
-    dependency: transitive
+    dependency: "direct main"
     description:
       name: collection
       sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
@@ -414,5 +414,5 @@ packages:
     source: hosted
     version: "6.2.2"
 sdks:
-  dart: ">=2.19.0 <3.0.0"
+  dart: ">=2.19.2 <3.0.0"
   flutter: ">=3.7.0-0"

+ 4 - 3
pubspec.yaml

@@ -1,9 +1,9 @@
 name: fooddeliveryapp
 description: A new Flutter application for food delivery.
-version: 1.2.3+1
+version: 1.2.9+1
 
 environment:
-  sdk: ">=2.7.0 <3.0.0"
+  sdk: '>=2.19.2 <3.0.0'
 
 dependencies:
   flutter:
@@ -14,7 +14,8 @@ dependencies:
   http: ^0.13.6
   shared_preferences: ^2.2.0
   fluttertoast: ^8.2.2
-  flutter_svg: ^2.0.5 # it help us to use SVG in our app
+  flutter_svg: ^2.0.5 # it help us to use SVG in our ap
+  collection: ^1.15.0-nullsafety.4p
 
 dev_dependencies:
   flutter_test: