否则颤抖

时间:2020-07-03 18:53:58

标签: flutter if-statement

我正在尝试创建一个If / else。但似乎我无法使用_local。谁能帮我为什么代码中断会导致_locale错误?

if(_locale = "da") {
    static const String FEED_URL = 'http://www.rssmix.com/u/1231445/rss.xml';
  } else {
    static const String FEED_URL = 'http://www.rssmix.com/u/87244544/rss.xml';
  }

总代码如下:

import 'package:flutter/material.dart';
import 'package:webfeed/webfeed.dart';
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:firebase_admob/firebase_admob.dart';
import 'package:brewingapp/app_localizations.dart';
import 'package:flutter/services.dart';
import 'package:devicelocale/devicelocale.dart';
import 'dart:io';

const String testDevice = "Mobile_id";
String _locale = "";
String locale;

class News extends StatefulWidget {
  News() : super ();
  final String title = 'Rss feed Demo';
  @override
  _NewsState createState() => _NewsState();
}

class _NewsState extends State<News> {

  static const MobileAdTargetingInfo targetingInfo = MobileAdTargetingInfo(
    testDevices: testDevice != null ? <String>[testDevice]: null,
    nonPersonalizedAds: true,
    keywords: <String>['beer', 'homebrew', 'brewing'],

  );

  BannerAd _bannerAd;

  BannerAd createBannerAd(){
    return BannerAd(
      //adUnitId: BannerAd.testAdUnitId,
        adUnitId: Platform.isAndroid
            ? 'ca-app-pub-9222131690243349/9303434192'
            : 'ca-app-pub-9225631690243349/9632223697',
        //adUnitId: ca-app-pub-3940252229222544/3200978111,
        size: AdSize.banner,
        targetingInfo: targetingInfo,
        listener: (MobileAdEvent event){
          print('BannerAd $event');

        }
    );
  }


  Future<void> initPlatformState() async {

    String currentLocale;



    try {
      currentLocale = await Devicelocale.currentLocale;
      print(currentLocale);
      locale = currentLocale;
    } on PlatformException {
      print("Error obtaining current locale");
    }


    if (!mounted) return;

    setState(() {

      _locale = currentLocale;



    });
  }

  String f = "";

  if(_locale = "da") {
    static const String FEED_URL = 'http://www.rssmix.com/u/1231445/rss.xml';
  } else {
    static const String FEED_URL = 'http://www.rssmix.com/u/87244544/rss.xml';
  }



  static const String FEED_URL = 'http://www.rssmix.com/u/11657742/rss.xml';
  RssFeed _feed;
  String _title;
  static const String loadingFeedMsg = 'Loading feed....';
  static const String feedLoadErrorMsg = 'Error loading feed';
  static const String feedOpenErrorMsg = 'Error opening Feed';
  static const String placeholderImg = 'images/no_image.png';
  GlobalKey<RefreshIndicatorState> _refreshKey;

  updateTitle(title){
    setState(() {
      _title = title;
    });
  }
  updateFeed(feed){
    setState(() {
      _feed = feed;
    });
  }

  Future<void> openFeed(String url) async {



    if(await canLaunch((url))){
      await launch(url, forceSafariVC: true, forceWebView: false,
      );
      return;

    }
    updateTitle(feedOpenErrorMsg);
  }

  load() async {
    updateTitle(loadingFeedMsg);
    loadFeed().then((result){
      if(null == result ||result.toString().isEmpty){
        updateTitle(feedLoadErrorMsg);
        return;

      }
      updateFeed(result);
      updateTitle(_feed.title);
    });
  }

  Future<RssFeed> loadFeed() async{
    try{
      final client = http.Client();
      final response = await client.get(FEED_URL);
      return RssFeed.parse(response.body);

    }catch (e){
      return null;

    }
  }


  @override
  void initState() {
    FirebaseAdMob.instance.initialize(
      //appId: BannerAd.testAdUnitId);
      appId: Platform.isAndroid
          ? 'ca-app-pub-9222131690243349~5688742813'
          : 'ca-app-pub-9222131690243349~9503902731',
    );
    _bannerAd = createBannerAd()..load()..show();
    initPlatformState();

    super.initState();

    _refreshKey = GlobalKey<RefreshIndicatorState>();
    updateTitle(widget.title);
    load();

  }

  title(title){
    return Text(
      title,
      style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w400),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  subtitle(subtitle){
    return Text(
      subtitle,
      style: TextStyle(fontSize: 11.0, fontWeight: FontWeight.w200),
      maxLines: 3,
      overflow: TextOverflow.ellipsis,
    );
  }

  thumbnail(imageUrl){
    return Padding(
      padding: EdgeInsets.only(left: 15.0),
      child: CachedNetworkImage(
        placeholder: (context, url) => Image.asset(placeholderImg),
        imageUrl: imageUrl,
        height: 50,
        width: 70,
        alignment: Alignment.center,
        fit: BoxFit.fill,
      ),
    );
  }

  rightIcon(){
    return Icon(
      Icons.keyboard_arrow_right, color: Colors.grey, size: 30.0,
    );
  }



  @override
  void dispose() {
    _bannerAd.dispose();
    super.dispose();
  }







  @override


  list(){
    return ListView.builder(
      itemCount: _feed.items.length,
      itemBuilder: (BuildContext context, int index){
        final item = _feed.items[index];
        //return ListTile(
        return ListTile(
          title: title(item.title),
          //subtitle: subtitle(item.description),
          subtitle: subtitle(item.description),


          leading: item.enclosure?.url != null
              ? thumbnail(item.enclosure.url)
              : SizedBox.shrink(),
          trailing: rightIcon(),
          contentPadding: EdgeInsets.all(5.0),
          onTap: () => openFeed(item.link),


        );

      },
    );
  }

  isFeedEmpty(){
    return null == _feed || null == _feed.items;
  }

  body(){
    return isFeedEmpty() ? Center(
      child: CircularProgressIndicator(),
    )
        :RefreshIndicator(
      key: _refreshKey,
      child: list(),
      onRefresh: () => load(),
    );



  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Ølbrygning nyheder"),
        backgroundColor: Colors.green[800],
      ),
      body:  body(),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

当我对其进行测试时,编译器只是说命名参数不应以“ _”开头,但是当我对其重命名时,我认为它可以正常工作。因此,也许将其重命名。

答案 1 :(得分:0)

=是分配运算符。您正在寻找等于运算符==

if(_locale == "da") {
    static const String FEED_URL = 'http://www.rssmix.com/u/1231445/rss.xml';
} else {
    static const String FEED_URL = 'http://www.rssmix.com/u/87244544/rss.xml';
}

要检查两个条件是否相等时,请使用==

a == 5 // Checks if a is equal to 5

要为变量分配值时,请使用=

a = 5 // Assigns a the value of 5