Flutter 应用程序仅在 Android 设备中崩溃

问题描述 投票:0回答:1

我真的需要帮助,我已经尝试了很多天,但仍然没有解决问题 当我在 ios 模拟器中运行这个应用程序时,它运行良好,但是当我在任何 Android 设备中运行时,它会立即崩溃,我没有遇到问题

应用程序启动后我的调试控制台崩溃

Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
✓ Built build/app/outputs/flutter-apk/app-debug.apk
D/FlutterGeolocator( 2637): Attaching Geolocator to activity
D/FlutterGeolocator( 2637): Creating service.
D/FlutterGeolocator( 2637): Binding to location service.
D/FlutterLocationService( 2637): Creating service.
D/FlutterLocationService( 2637): Binding to location service.
D/FlutterGeolocator( 2637): Geolocator foreground service connected
D/FlutterGeolocator( 2637): Initializing Geolocator services
D/FlutterGeolocator( 2637): Flutter engine connected. Connected engine count 1
Connecting to VM Service at ws://127.0.0.1:63706/2UBZY5eep1A=/ws
Connected to the VM Service.
I/flutter ( 2637): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmU3ZmEwMjliZGZlMTAwNmUzOTY0MzkiLCJpYXQiOjE3MjcxNzMyNTMsImV4cCI6MTc0Mjk0MTI1MywidHlwZSI6ImFjY2VzcyJ9.b0PWqexRxdiypAdbi8mdn14JQaYdLPerEryw3k1sVTU
I/flutter ( 2637): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmU3ZmEwMjliZGZlMTAwNmUzOTY0MzkiLCJpYXQiOjE3MjcxNzMyNTMsImV4cCI6MTc1ODcwOTI1MywidHlwZSI6InJlZnJlc2gifQ.xu95VDDLfimL0nAiMvM0ZD2R6REsxYAOj5ASR1WMnBw
D/EGL_emulation( 2637): app_time_stats: avg=12.65ms min=3.00ms max=120.38ms count=50
D/EGL_emulation( 2637): app_time_stats: avg=4.20ms min=1.37ms max=8.33ms count=61
D/ProfileInstaller( 2637): Installing profile for com.example.vezigo
D/EGL_emulation( 2637): app_time_stats: avg=6.14ms min=1.10ms max=199.55ms count=57
Reloaded 1 of 1157 libraries in 243ms.

重启后

E/flutter ( 2637): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: TimeoutException after 0:00:30.000000: Future not completed
E/flutter ( 2637): 
Restarted application in 617ms.
W/WindowOnBackDispatcher( 2637): sendCancelIfRunning: isInProgress=false callback=io.flutter.embedding.android.FlutterActivity$1@bf7ddac
I/flutter ( 2637): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmU3ZmEwMjliZGZlMTAwNmUzOTY0MzkiLCJpYXQiOjE3MjcxNzMyNTMsImV4cCI6MTc0Mjk0MTI1MywidHlwZSI6ImFjY2VzcyJ9.b0PWqexRxdiypAdbi8mdn14JQaYdLPerEryw3k1sVTU
I/flutter ( 2637): eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmU3ZmEwMjliZGZlMTAwNmUzOTY0MzkiLCJpYXQiOjE3MjcxNzMyNTMsImV4cCI6MTc1ODcwOTI1MywidHlwZSI6InJlZnJlc2gifQ.xu95VDDLfimL0nAiMvM0ZD2R6REsxYAOj5ASR1WMnBw
D/EGL_emulation( 2637): app_time_stats: avg=61040.50ms min=1.08ms max=244092.02ms count=4
D/EGL_emulation( 2637): app_time_stats: avg=3.28ms min=0.89ms max=9.21ms count=61
D/EGL_emulation( 2637): app_time_stats: avg=2.55ms min=0.96ms max=6.38ms count=60
D/EGL_emulation( 2637): app_time_stats: avg=5.00ms min=1.14ms max=163.79ms count=59

扑医生

arleven@Arlevens-Mini vezigo-flutter % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, 3.24.1-0.0.pre.3, on macOS 14.6.1 23G93 darwin-arm64, locale en-IN)
    ! Upstream repository https://github.com/tanuu007/flutter.git is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to https://github.com/tanuu007/flutter.git to dismiss this error.
[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/to/macos-android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.93.1)
[✓] Connected device (5 available)
    ! Error: Browsing on the local area network for Arleven’s iPad. Ensure the device is unlocked and attached with a cable or associated with the same local area
      network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

! Doctor found issues in 2 categories.

还有我的主屏幕,如果出现任何问题,应用程序就会崩溃

import 'package:flutter/material.dart';
import 'package:vezigo/Api_Models/banner.dart';
import 'package:vezigo/Screens/categories.dart';
import 'package:vezigo/Models/colors.dart';
import 'package:vezigo/Screens/cart_screen.dart';
import 'package:vezigo/Providers/item_provider.dart';
import 'package:provider/provider.dart';
import 'package:vezigo/Models/category.dart';
import 'dart:async';
import 'package:badges/badges.dart' as badges;
import 'package:http/http.dart' as http;
import 'package:vezigo/Screens/search.dart';
import 'dart:convert';
import '../Api_Models/list_products.dart';
import '../Providers/fav_provider.dart';
import 'item_details.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';

class MyHomeScreen extends StatefulWidget {
  const MyHomeScreen({super.key});

  @override
  State<MyHomeScreen> createState() => _MyHomeScreenState();
}
class _MyHomeScreenState extends State<MyHomeScreen> {
  int _currentPage = 0;
  final TextEditingController _searchController = TextEditingController();
  final ScrollController _scrollController = ScrollController();
  String _appBarTitle = 'Vezigo';
  bool _isSearchVisible = false;
 final PageController _pageController = PageController();
  Timer? _timer;

  @override
  void initState() {
    super.initState();
    _futureProducts = fetchProducts();
    _futureBanners = fetchbanner();
    _scrollController.addListener(_scrollListener);
  _timer = Timer.periodic(const Duration(seconds: 3), (Timer timer) {
  if (_pageController.hasClients) {
    if (_currentPage < 2) {
      _currentPage++;
    } else {
      _currentPage = 0;
    }
    _pageController.animateToPage(
      _currentPage,
      duration: const Duration(milliseconds: 300),
      curve: Curves.easeIn,
    );
  }
});

  }
  @override
  void dispose() {
    _scrollController.dispose();
    _timer?.cancel();
    _pageController.dispose();
    super.dispose();
  }
  void _scrollListener() {
    double offset = _scrollController.offset;
    if (offset > 400) {
      setState(() {
        _appBarTitle = 'Fruits';
      });
    } else {
      setState(() {
        _appBarTitle = 'Vezigo';
      });
    }
  }
  Future<List<Product>> fetchProducts() async {
  final response = await http.get(Uri.parse('${AppColors.api}/products'));
  if (response.statusCode == 200) {
    final Map<String, dynamic> responseMap = json.decode(response.body);
    final Map<String, dynamic> dataMap = responseMap['data'];
    final List<dynamic> productList = dataMap['results'];
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<String> productIds = productList.map((json) => Product.fromJson(json).id).toList();
    await prefs.setStringList('productIds', productIds);
    return productList.map((json) => Product.fromJson(json)).toList();
  } else {
    throw Exception('Failed to load products');
  }
}

//Banner Api
Future<List<BannerItem>> fetchbanner() async{
  final response = await http.get(Uri.parse('${AppColors.api}/app/banners'));
  if(response.statusCode == 200){
    final Map<String, dynamic> responseMap = json.decode(response.body);
    final Map<String, dynamic> dataMap = responseMap['data'];
    final List<dynamic> bannerData = dataMap['results'];
    return bannerData.map((json) => BannerItem.fromJson(json)).toList();

  } else {
    throw Exception('Failed to load banners');
  }
}
late Future<List<Product>> _futureProducts;
late Future<List<BannerItem>> _futureBanners;
   @override
  Widget build(BuildContext context) {
     final favoriteProvider = Provider.of<FavoriteProvider>(context);
    final cartProvider = Provider.of<Cart>(context);
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text(
          _appBarTitle,
          style: const TextStyle(color: AppColors.textColor),
        ),
        centerTitle: true,
        backgroundColor: AppColors.appbarColor,
        scrolledUnderElevation: 0,
        leading: const SizedBox(width: 70),
        actions: [
          IconButton(
            onPressed: () {
              setState(() {
                _isSearchVisible = !_isSearchVisible;
              });
            },
            icon: const Icon(Icons.search, size: 30, color: AppColors.textColor),
          ),
          badges.Badge(
            position: badges.BadgePosition.topEnd(top: 0, end: 3),
            badgeStyle:const badges.BadgeStyle(
              badgeColor: Colors.white,
            ),
            badgeAnimation: const badges.BadgeAnimation.fade(
              animationDuration: Duration(milliseconds: 300),
            ),
            showBadge: cartProvider.items.isNotEmpty,
            badgeContent: Text(
              cartProvider.items.length.toString(),
              style: const TextStyle(color: Colors.black, fontSize: 12),
            ),
            child: IconButton(
              tooltip: 'cart',
              icon: const Icon(Icons.shopping_cart, size: 30, color: AppColors.textColor),
              onPressed: () {
                Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => const CartScreen()),
                );
              },
            ),
          ),
        ],
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          color: AppColors.appbarColor,
        ),
        child: SafeArea(
          child: SingleChildScrollView(
            controller: _scrollController,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Visibility(
                  visible: _isSearchVisible,
                  child: Padding(
                    padding:
                        const EdgeInsets.symmetric(horizontal: 18.0, vertical: 10.0),
                    child: TextField(
                      readOnly: true,
                      onTap: ()async {
                            List<Product> productList = await _futureProducts; 
                        Navigator.of(context).push(MaterialPageRoute(builder: (ctx) => SearchScreen(productList: productList)));
                      },
                      controller: _searchController,
                      decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        labelText: 'Search Your Groceries',
                        suffixIcon: const Icon(Icons.search, color: AppColors.appbarColor),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(50),
                        ),
                      ),
                    ),
                  ),
                ),
                FutureBuilder<List<BannerItem>>(
                  future: _futureBanners,
                  builder: (context, snapshot){
                    if(snapshot.connectionState == ConnectionState.waiting){
                      return Center(child: CircularProgressIndicator(),);
                    } else if (snapshot.hasError) {
                       return Center(child: Text('Error: ${snapshot.error}'));
                      }else if (!snapshot.hasData || snapshot.data!.isEmpty) {
                        return const Center(child: Text('No products available'));
                       } else {
                        final banners = snapshot.data!;  
                  return AnimatedSize(
                    duration: const Duration(milliseconds: 300),
                    child: Container(
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: AppColors.backgroundColor,
                        borderRadius: BorderRadius.circular(0),
                      ),
                      child: Padding(
                        padding: const EdgeInsets.only(top: 10.0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                          Container(
  width: double.infinity,
  height: MediaQuery.of(context).size.height * 0.2,
  decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(30),
    color: const Color.fromARGB(255, 249, 255, 249),
  ),
  child: PageView.builder(
    controller: _pageController,
    onPageChanged: (index) {
      setState(() {
        _currentPage = index;
      });
    },
    itemCount: banners.length,
    itemBuilder: (context, index) {
      return GestureDetector(
        
        onTap: () async {
              final url = Uri.parse(banners[index].url);
           if (await canLaunchUrl(url)) {
            await launchUrl(url, mode: LaunchMode.externalApplication);
          } else {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(content: Text('Could not launch $url')),
            );
          }
        },
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 10.0),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(30),
            color: AppColors.lightTheme,
          ),
          clipBehavior: Clip.hardEdge, 
          child: Image.network(
            banners[index].image,
            fit: BoxFit.cover,
            width: double.infinity,
            height: double.infinity, 
            
          ),
        ),
      );
    },
  ),
),
const SizedBox(height: 10),
Row(
  mainAxisAlignment: MainAxisAlignment.center,
  children: List.generate(banners.length, (index) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 4),
      width: 10,
      height: 10,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        color: _currentPage == index
            ? AppColors.buttonColor
            : Colors.green.shade200,
      ),
    );
  }),
),

                         const   SizedBox(height: 10,),
                            const Padding(
                              padding: EdgeInsets.symmetric(horizontal: 16.0),
                              child: Text(
                                'Categories',
                                style: TextStyle(
                                    fontSize: 20, fontWeight: FontWeight.bold),
                              ),
                            ),
                           Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: SingleChildScrollView(
                      scrollDirection: Axis.horizontal,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          CategoryItem(
                            title: 'Fruits',
                            imagePath: 'assets/images/orange.png',
                            onPressed: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                  builder: (context) =>const CategoriesScreen(
                    categoryTitle: 'fruit',
                  ),
                                ),
                              );
                            },
                          ),
                          const SizedBox(width: 10),
                          CategoryItem(
                            title: 'Veggies',
                            imagePath: 'assets/images/veggies.png',
                            onPressed: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                  builder: (context) =>const CategoriesScreen(
                    categoryTitle: 'vegetable',
                  ),
                                ),
                              );
                            },
                          ),
                          const SizedBox(width: 10),
                  
                        ],
                      ),
                    ),
                  ),
                  
                            const SizedBox(height: 20),
                            const Padding(
                              padding: EdgeInsets.symmetric(horizontal: 16.0),
                              child: Text(
                                'Popular',
                                style: TextStyle(
                                    fontSize: 20, fontWeight: FontWeight.bold),
                              ),
                            ),
                           
                            FutureBuilder<List<Product>>(
                          future: _futureProducts,
                          
                          builder: (context, snapshot) {
                            if (snapshot.connectionState == ConnectionState.waiting) {
                              return const Center(child: CircularProgressIndicator());
                            } else if (snapshot.hasError) {
                              return Center(child: Text('Error: ${snapshot.error}'));
                            } else if (!snapshot.hasData || snapshot.data!.isEmpty) {
                              return const Center(child: Text('No products available'));
                            } else {
                              final products = snapshot.data!;
                              return Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: GridView.builder(
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    crossAxisSpacing: 10.0,
                    mainAxisSpacing: 10.0,
                    childAspectRatio: 0.99,
                  ),
                  itemCount: products.length,
                  itemBuilder: (context, index) {
                    final product = products[index];
                    final isFavorite = favoriteProvider.isFavorite(product);
                  
                    return GestureDetector(
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => ItemDetailsScreen(productId: product.id),
                          ),
                        );
                      },
                      child: Card(
                        color: Colors.white,
                        elevation: 5,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(15),
                        ),
                        child: Container(
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20),
                            border: Border.all(
                              color: Colors.white,
                              width: 5,
                            ),
                          ),
                          child: Stack(
                            children: [
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Flexible(
                                    flex: 2,
                                    child: ClipRRect(
                                      borderRadius: const BorderRadius.vertical(top: Radius.circular(15)),
                                      child: Container(
                                        color: AppColors.lightTheme,
                                        child: Image.network(
                                          product.imageUrl,
                                          fit: BoxFit.cover,
                                          width: double.infinity,
                                        ),
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          product.title,
                                          style: const TextStyle(
                                            fontSize: 16,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                        RichText(
                                          text: TextSpan(
                                            children: <TextSpan>[
                                              TextSpan(
                                                text: '₹${product.marketPrice}',
                                                style: TextStyle(
                                                  fontSize: 16,
                                                  fontWeight: FontWeight.bold,
                                                  color: AppColors.priceColor,
                                                ),
                                              ),
                                             
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                              Positioned(
                                top: 8,
                                right: 8,
                                child: ElevatedButton(
                                  style: ElevatedButton.styleFrom(
                                    backgroundColor: AppColors.lightTheme,
                                    shape: const CircleBorder(),
                                  ),
                                  child: Icon(
                                    isFavorite ? Icons.favorite : Icons.favorite_border,
                                    color: AppColors.buttonColor,
                                  ),
                                  onPressed: () {
                                    favoriteProvider.toggleFavorite(product);
                                  },
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    );
                  },
                                ),
                              );
                            }
                          },
                        ),
                  
                          ],
                        ),
                      ),
                    ),
                  );
  }}),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

此外,当我运行 flutter run 命令时,应用程序运行正常并且一切正常 请任何人都可以帮助我很多天了

android flutter dart api
1个回答
0
投票

尝试在 Android 设备上启用开发者模式,Flutter 医生这样说。

© www.soinside.com 2019 - 2024. All rights reserved.